asp.net - Unable to cast object of type 'system.string' to type 'system.Byte[]' when trying to download a file from database -


i trying download file database giving me error called unable cast object of type 'system.string' type 'system.byte[]' on line : response.addheader("content-length", bytes.tostring());

please tell me how can cast string byte in case, thank in advance.

i have column named salefilename want download file.

aspx code:

 <asp:templatefield headertext="recieptname" sortexpression="recieptname">                                 <itemtemplate>                                     <asp:linkbutton id="linkbutton1" runat="server" commandname="download" commandargument='<%# bind("salename") %>' text='<%# bind("salename") %>' ></asp:linkbutton>                                 </itemtemplate>                             </asp:templatefield> 

code behind file:

 protected void gridcontributions_rowcommand(object sender, gridviewcommandeventargs e)     {         if (e.commandname == "download")         {              // make sure filename  contains file name 'test.pdf'               string filename = convert.tostring(e.commandargument);              // make sure filepath  contains file path 'uploads/scheme/test.pdf'                string filepath = e.commandargument.tostring();               string connectionstring = webconfigurationmanager.connectionstrings["connectionstring2"].connectionstring;             // sqlconnection con = new sqlconnection(connectionstring);             // byte[] bytes;             //string contenttype;              using (sqlconnection con = new sqlconnection(connectionstring))             {                 using (sqlcommand cmd = new sqlcommand())                 {                     cmd.commandtext = "selectsalefilename contributions salefilename = @salefilename";                     cmd.parameters.addwithvalue("@salefilename", filename);                     //d.commandtype = commandtype.storedprocedure;                     cmd.connection = con;                     con.open();                     using ( sqldatareader sdr = cmd.executereader())                      {                         sdr.read();                         bytes = (byte[])sdr["salefilename"];                        //byte data = system.text.encoding.unicode.getbytes(salefilename.tostring);                        }                     con.close();                  }             }             response.clear();             response.buffer = true;              // read original file disk              filestream myfilestream = new filestream( filepath  ,filemode.open);             long filesize = myfilestream.length;             byte[] buffer = new byte[convert.toint32(filesize)];             myfilestream.read(buffer, 0, convert.toint32(filesize));             myfilestream.close();                // // tell browse stuff file             response.addheader("content-length", bytes.tostring());            // response.addheader("content-length", filenam                 //response.addheader("content-disposition", "inline; filename=" & filename.replace(" ", "_"));             response.addheader("content-disposition", "attachment; filename=" + filename + ";");             response.transmitfile(filename);             response.contenttype = "application/octet-stream";              // send data browser             response.binarywrite(buffer);             response.end();         }     } 

you can't cast byte[] string (unless textual data , should encode data using encoding unicode, ascii)

anyway, problem in content-length on http header, should length of file

so replace line:

response.addheader("content-length", bytes.tostring());

with this:

response.addheader("content-length", filesize.tostring());

if file stored databaase need byte array, following code you:

private byte[] readfilefromdatabase(string filename) {     string connectionstring = webconfigurationmanager.connectionstrings["connectionstring2"].connectionstring;       byte[] bytes = null;      using (sqlconnection con = new sqlconnection(connectionstring))     {         using (sqlcommand cmd = new sqlcommand())         {             cmd.commandtext = "selectsalefilename contributions salefilename = @salefilename";             cmd.parameters.addwithvalue("@salefilename", filename);             cmd.connection = con;             con.open();              using ( sqldatareader sdr = cmd.executereader())             {                if (sdr.read() )                   bytes = (byte[])sdr["salefilename"];             }             con.close();         }     }      return bytes; }  protected void gridcontributions_rowcommand(object sender, gridviewcommandeventargs e) {     if (e.commandname == "download")     {         string filename = convert.tostring(e.commandargument);               byte[] bytes = readfilefromdatabase(filename);          response.clear()         response.contenttype = "application/octet-stream"         response.addheader("content-disposition", "attachment; filename=" + filename + ";");         response.binarywrite(bytes)         response.end()     } } 

if file stored outside database:

public static void downloadfile(string path, string contenttype) {     fileinfo file = new fileinfo(path);     if (file.exists)     {                         response.clear();         response.clearheaders();         response.clearcontent();          response.addheader("content-type", contenttype);         response.addheader("content-disposition", "attachment; filename=" + file.name);         response.addheader("content-length", file.length.tostring());             response.flush();         response.transmitfile(file.fullname);         response.end();     } } 

call as:

string fullpath = server.mappath(relativepath); downloadfile(fullpath , "application/octet-stream"); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -