Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Dear All

This is the code I have used in my page to download the file from the database. I have stored in the varbinary format. When i clicked the linkbutton , the file has to open.but I'm getting the javscript error. Please help me to sort out this.

byte[] fileData =(byte[])ds.Tables[0].Rows[0][3];
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment; filename=1.xls"); // + ds.Tables[0].Rows[0][2]);
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(Response.OutputStream);
            bw.Write(fileData);
            bw.Close();
            Response.ContentType =ds.Tables[0].Rows[0][4].ToString()  ;
            Response.End();



The problem is that fileupload control kept inside the ajax updatepanel control. Now its working when I placed the control outside of the panel. But I need to know what's the reason.

Regards
Sheik
Posted
Updated 29-Nov-11 22:57pm
v2
Comments
thatraja 23-Nov-11 11:56am    
Always mention the error messages in your question.
:(

What javascript error?

This is how I have done it in the past, with no errors:

byte[] fileData =(byte[])ds.Tables[0].Rows[0][3];
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.Buffer = true;
context.Response.ContentType = ds.Tables[0].Rows[0][4].ToString()  ;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=1.xls");
context.Response.AppendHeader("content-length", filedata.Length.ToString());
context.Response.BinaryWrite(filedata);
context.Response.Flush();
context.Response.End();


(Looking at it now, I think the ClearContent and ClearHeaders might be redundant after the Clear. :-p )
 
Share this answer
 
v2
You didn't provide complete details in your question. Already answered a similar question here before in Q/A section, check it.

Save and Retrieve Files from Database[^]
 
Share this answer
 
C#
string style = @"<style> .text { mso-number-format:\@; } </style> ";
GridView1.Visible = true;
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=file.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//GridView1.RenderControl(hw);
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();


C#
public override void VerifyRenderingInServerForm(Control GridView1)
  {

      /* Verifies that the control is rendered */

  }
 
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