如何从数据库下载文件 [英] How to download the file from the database
问题描述
亲爱的所有人
这是我在页面中用来从数据库下载文件的代码.我已经以varbinary格式存储了.当我单击linkbutton时,文件必须打开.但是我遇到了javscript错误.请帮助我解决这个问题.
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();
问题在于fileupload控件保留在ajax updatepanel控件内.现在,当我将控件放置在面板之外时,它就可以工作了.但是我需要知道是什么原因.
问候
Sheik
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
推荐答案
什么是JavaScript错误?
这是我过去所做的事情,没有错误:
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();
(现在来看,我认为Clear清除后,ClearContent和ClearHeaders可能是多余的.:-p)
(Looking at it now, I think the ClearContent and ClearHeaders might be redundant after the Clear. :-p )
您没有在问题中提供完整的细节.之前在问答"部分已经回答了类似的问题,请进行检查.
从数据库中保存和检索文件 [
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[^]
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();
public override void VerifyRenderingInServerForm(Control GridView1)
{
/* Verifies that the control is rendered */
}
这篇关于如何从数据库下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!