ASP.NET - how to download image - Asked By msakt on 26-Jun-12 04:14 AM

in table have binary data(image is convert into binary)

in ui have grid if click button download particular image


S K replied to msakt on 26-Jun-12 04:20 AM
Simply get the binary data from table an pass it into this method to download the image

private void DownloadFile(string imageName, byte[] binaryData)
{
    Response.ClearHeaders();
    Response.ClearContent();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + imageName);
    Response.BinaryWrite(binaryData);
    Response.End();
}


hope this helps you
Chintan Vaghela replied to msakt on 26-Jun-12 04:29 AM

Hi Frndz,

 

Functionality:  Download Binary Data

 

To achieve this task,

 

Response.ContentType = "application/octet-stream";

      Response.AddHeader("Content-Disposition", "attachment; filename=" + imageName);

      Response.BinaryWrite(binaryData);

 

Update logic as following way

 

 

Full Logic     :

 

 

protected void btnDownload_Click(object sender, EventArgs e)

    {

 

 

     

      Response.ClearHeaders();

      Response.ClearContent();

      Response.ContentType = "application/octet-stream";

      Response.AddHeader("Content-Disposition", "attachment; filename=" + imageName);

      Response.BinaryWrite(binaryData);

      Response.End();

    }

 

Hope this helpful!

Thanks

 

 

 

Ravi Maurya replied to msakt on 26-Jun-12 04:30 AM
Function to get the file from the database

public static byte[] GetFileFromDB(string id)
  {
    object[] retVal = null;
    byte[] file = null;
    SqlParameter[] param = new SqlParameter[]
    {
      new SqlParameter ("@id", id)
    };
    string _connString = ConfigurationManager.ConnectionStrings[<ConnectionStringName>].ConnectionString.ToString();
    SqlDataReader reader = SqlAccessor.ExecuteReader(_connString, SqlAccessor.SqlCommandBuilder(new SqlCommand("SourceFileLogSelFileDataProc"), param), CommandType.StoredProcedure, out retVal);
 
    if (reader.Read())
    {
      file = (byte[])reader["<varbinary column that we define in Step 1>"];
    }
 
    return file;
  }
Code to download the file

byte[] fileData = Utils.GetFileFromDB(<ID>);
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + <filename>);
BinaryWriter bw = new BinaryWriter(Response.OutputStream);
bw.Write(fileData);
bw.Close();
Response.ContentType = ReturnExtension("image/jpeg");
Response.End();


For complete code refer this link

http://weblogs.sqlteam.com/randyp/archive/2008/11/20/60771.aspx
Jitendra Faye replied to msakt on 26-Jun-12 04:36 AM
For this implement RowCommand event like this-

protected void grdFileDetails_RowCommand(object sender, GridViewCommandEventArgs e)
      {
        try
        {
          //Code to download file
          if (e.CommandName == "download")
          {
            string filepath = Convert.ToString(e.CommandArgument);

            byte[] data = System.IO.File.ReadAllBytes(filepath);
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "Application/octet-stream");
            Response.AddHeader("Content-Length", data.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filepath);
            Response.BinaryWrite(data);
            Response.End();
          }

        }

        catch (Exception)
        {
                 throw;
        }
      }

Follow my article for this-

http://steptodotnet.blogspot.in/2012/06/download-file-using-gridview-in-aspnet.html

DL M replied to msakt on 26-Jun-12 04:40 AM
Use  this code
-----------
private void download (DataTable dt)

{

    Byte[] bytes = (Byte[])dt.Rows[0]["Data"];

    Response.Buffer = true;

    Response.Charset = "";

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    Response.ContentType = dt.Rows[0]["ContentType"].ToString();

    Response.AddHeader("content-disposition", "attachment;filename="

    + dt.Rows[0]["Name"].ToString());

    Response.BinaryWrite(bytes);

    Response.Flush();

    Response.End();

}

read more:->http://forums.asp.net/t/1528623.aspx
dipa ahuja replied to msakt on 26-Jun-12 08:02 AM
1. Add a LinkButton with Download Command Name
<asp:Label ID="lblFileID" runat="server" Text='<%# Eval("Fileid") %>'/>
<asp:LinkButton ID="lblFileName" CommandName="Download" runat="server" Text="Download" />

2. Implement the Gridview RowCommandEvent:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{     
  if (e.CommandName == "Download")
  {
    GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
    string strRequest = ((Label)row.FindControl("lblFileID")).Text;
 
    if (strRequest != "")
    {
      string path = Server.MapPath(strRequest); //get file object as FileInfo  
      System.IO.FileInfo file = new System.IO.FileInfo(path); //-
      if (file.Exists) //set appropriate headers  
      {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
 
        // write file to browser
        Response.WriteFile(file.FullName);
        Response.End();
      }
      else
      {
        // if file does not exist
        Response.Write("This file does not exist.");
      }
    }
    else
    {
      //nothing in the URL as HTTP GET
      Response.Write("Please provide a file to download.");
    }
 
  }
}

Done!