Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I Developed an application for Uploading Files and Downloading Files,
I used database to store my document file in which, Documents are stored in
Byte array,

Now i am unable to convert back that byte array into its original file format.

can any one help me to solve out this problem?

i tried following code for download:

C#
{
  HttpContext.Current.Response.Clear();
  HttpContext.Current.Response.AddHeader("Content-Disposition",     
     "attachment; filename=" + fileName);
  HttpContext.Current.Response.AddHeader("Content-Length",   
     Convert.ToString(value.Length));
  HttpContext.Current.Response.ContentType = "application/octet-stream";
  HttpContext.Current.Response.Write(value);
  HttpContext.Current.Response.End();}



help me if any one get any idea!!!
Posted
Comments
Timberbird 25-Aug-11 3:18am    
How do you upload file? Do you read bytes from stream or converting base64 to byte array?

Don't use Response.Write, use Response.BinaryWrite instead.

The way I handle it is to use an ASPX file:
C#
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wm5ftdl.aspx.cs" Inherits="wm5ftdl" %>

<%
    // Send a download file to the client given the filename.
    string guid = Request.QueryString["file"];
    string fileName = "ERROR";
    byte[] data = new byte[] { 0, 0, 0, 0 };
    string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
        {
        con.Open();
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
                        "FROM dlContent cn " +

                        "WHERE cn.iD=@ID";
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
            {
            cmd.Parameters.AddWithValue("@ID", guid);
            using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                if (r.Read())
                    {
                    fileName = (string) r["filename"];
                    data = (byte[]) r["dataContent"];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Content-Description", "File Download");
    Response.AddHeader("Content-Type", "application/force-download");
    Response.AddHeader("Content-Transfer-Encoding", "binary\n");
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(data);
    Response.End();
%>
 
Share this answer
 
Comments
Carmineeeeee 5-Sep-18 8:47am    
Hi,

how can I solve if I do not know the name and especially the file extension?

Thanks
Hi OriginalGriff,

when i puts Response.BinaryWrite,
it works same as Response.Write and
again it give wrong format of data

and gives "System Bytes[]" as output when i opens it with notepad.

Regards,
tushar
 
Share this answer
 
Comments
Timberbird 25-Aug-11 4:26am    
Er... do you attempt to initialize your value in a way like value = myBinaryData.ToString();?
I got Solution of this problem using following code:

C#
HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + FileName + "\"");
            HttpContext.Current.Response.AddHeader("Content-Length", Convert.ToString(Content.Length));
            HttpContext.Current.Response.BinaryWrite(Content);
            HttpContext.Current.Response.End();


and i set data type of file to binary.

thank you all for help!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900