Thursday, August 26, 2010

Programatically upload files to remote server

FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
 
try
{
    //Settings required to establish a connection with the server
    this.ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ServerIP/FileName"));
    this.ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
    this.ftpRequest.Proxy = null;
    this.ftpRequest.UseBinary = true;
    this.ftpRequest.Credentials = new NetworkCredential("UserName", "Password");
 
    //Selection of file to be uploaded
    FileInfo ff = new FileInfo("File Local Path With File Name"); //e.g.: c:\\Test.txt
    byte[] fileContents = new byte[ff.Length];
 
    //will destroy the object immediately after being used
    using (FileStream fr = ff.OpenRead())
    {
        fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
    }
 
    using (Stream writer = ftpRequest.GetRequestStream())
    {
        writer.Write(fileContents, 0, fileContents.Length);
    }
    //Gets the FtpWebResponse of the uploading operation
    this.ftpResponse = (FtpWebResponse)this.ftpRequest.GetResponse();
    Response.Write(this.ftpResponse.StatusDescription); //Display response
}
catch (WebException webex)
{
    this.Message = webex.ToString();
}

how to Download file from remote computer

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Net;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //base.OnLoad(e);
        string url = string.Empty;// Request.QueryString["DownloadUrl"];
        if (url == null || url.Length == 0)
        {
            url = "http://img444.imageshack.us/img444/6228/initialgridsq7.jpg";
        }
        //Initialize the input stream
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        int bufferSize = 1;
        //Initialize the output stream
        Response.Clear();
        Response.AppendHeader("Content-Disposition:", "attachment; filename=download.jpg");
        Response.AppendHeader("Content-Length", resp.ContentLength.ToString());
        Response.ContentType = "application/download";
        //Populate the output stream
        byte[] ByteBuffer = new byte[bufferSize + 1];
        MemoryStream ms = new MemoryStream(ByteBuffer, true);
        Stream rs = req.GetResponse().GetResponseStream();
        byte[] bytes = new byte[bufferSize + 1];
        while (rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0)
        {
            Response.BinaryWrite(ms.ToArray());
            Response.Flush();
        }
        //Cleanup
        Response.End();
        ms.Close();
        ms.Dispose();
        rs.Dispose();
        ByteBuffer = null;
    }
}

Wednesday, August 25, 2010

How to select Gridview Row in JavaScript

<script language="javascript" type="text/javascript">
var oldgridSelectedColor;

function setMouseOverColor(element)
{
    oldgridSelectedColor = element.style.backgroundColor;
    element.style.backgroundColor='yellow';
    element.style.cursor='hand';
    element.style.textDecoration='underline';
}

function setMouseOutColor(element)
{
    element.style.backgroundColor=oldgridSelectedColor;
    element.style.textDecoration='none';
}
</script>
protected void GridView1_RowDataBound(object sender, 
                    GridViewRowEventArgs e)
    {
        //check the item being bound is actually a DataRow, if it is, 
        //wire up the required html events and attach the relevant JavaScripts
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = 
                "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = 
                "javascript:setMouseOutColor(this);";
            e.Row.Attributes["onclick"] = 
            ClientScript.GetPostBackClientHyperlink
                (this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }

Image Cache

Response.Clear()
        Response.ContentType = "image/jpg"
        Dim imgBytes As Byte() = CType(Cache("ImageBytes"), Byte())
        If imgBytes Is Nothing Then
        Using img As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("TestImage.jpg"))
            Dim ms As IO.MemoryStream = New IO.MemoryStream()
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            ms.Close()
            imgBytes = ms.GetBuffer()
            Cache("ImageBytes") = imgBytes
        End Using
        Response.OutputStream.Write(imgBytes, 0, imgBytes.Length)
        Response.Flush()
        Response.End()

Saturday, July 31, 2010

How to get Row Index at Row Command event of GridView

 If e.CommandName.Equals("Save") Then
            Dim gvr As GridViewRow = CType(CType(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
            Dim RowIndex As Integer = gvr.RowIndex
End If

Thursday, July 1, 2010

Function to show pressed key in javascript

function show_key(e)
{ 
 
KeyID= Window.Event ? e.KeyCode : e.Which;
switch(KeyID)

   {

      case 16:

      document.Form1.KeyName.value = "Shift";

      break; 

      case 17:

      document.Form1.KeyName.value = "Ctrl";

      break;

      case 18:

      document.Form1.KeyName.value = "Alt";

      break;

      case 19:

      document.Form1.KeyName.value = "Pause";

      break;

      case 37:

      document.Form1.KeyName.value = "Arrow Left";

      break;

      case 38:

      document.Form1.KeyName.value = "Arrow Up";

      break;

      case 39:

      document.Form1.KeyName.value = "Arrow Right";

      break;

      case 40:

      document.Form1.KeyName.value = "Arrow Down";

      break;
   }

}

Wednesday, June 30, 2010

Function to Select or Deselect all checkbox in a gridview through javascript

<script language="javascript" type="text/javascript" >
function SelectAll(SelBtn)
     {
          var gvET = document.getElementById("GridView1"); 
             var rCount = gvET.rows.length;
             var btnSel = SelBtn.value;
             var rowIdx;
             // check the Page Count is greater than 1 then rowindex will start from 2 else 1
             if(document.getElementById("hidgridpagecount").value>1)
             {
                  rowIdx=2;
             }
             else
             {
                  rowIdx=1;
             }
             for (rowIdx; rowIdx<=rCount-1; rowIdx++)
             {
                  var rowElement = gvET.rows[rowIdx];
                  var chkBox = rowElement.cells[0].firstChild;
                  if (btnSel == 'Select All')
                  {
                       chkBox.checked = true;
                  }
                  else
                  {
                       chkBox.checked = false;
                  }
             }
             if (btnSel == 'Select All')
             {
                  document.getElementById('btnSelAll_Top').value ="Deselect All";
                  document.getElementById('btnSelAll_Bot').value ="Deselect All";
             }
             else
             {
                  document.getElementById('btnSelAll_Top').value ="Select All";
                  document.getElementById('btnSelAll_Bot').value ="Select All";
             }
             return false;
     }
</script>