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()