2010-08-05 135 views
0

我正在使用ASP.NET和C#。在ASP.NET中检索上传的文件

我已经做了一个文件上传页面,其中使用可以上传他们的文件。我保存在数据库中的三个字段,

  1. 文档名称[NVARCHAR]
  2. 文件[图片]
  3. DocumentType [NVARCHAR]

现在,我可以在添加记录数据库,成功。现在我想在gridview中显示它,例如DocumentName,DocumentType和下载文件的链接。

我试图通过检索记录并将它们分配给gridview,但我只得到两列。

+0

我认为你需要手动创建下载文件的链接。我不认为'GridView'可以自动为你做这个。 – bzlm 2010-08-05 10:30:42

+0

我创造了这个: Rahul 2010-08-05 10:36:31

+0

看起来不错。它工作吗? – bzlm 2010-08-05 10:49:04

回答

0

您必须创建一个下载处理程序来为实际文件提供服务。

处理程序可以从表中检索二进制文件,并直接写入输出流。然后

网格视图将指向此处理

+0

您能否请我重温我的任何example.I不知道如何创建下载处理程序。 – Rahul 2010-08-05 10:37:34

+0

我可以使用它吗? http://aspnetupload.com/Quickstart/Download-From-File.aspx 我会在linkbutton click事件中编写代码。 – Rahul 2010-08-05 10:39:26

0

既然你是存储DB文件,你将不得不编写代码来读取文件数据,添加适当的头,做RESPONSE.WRITE

例如

你的LinkBut​​ton的点击处理程序的代码会像

private void lnkDownload_Click(object sender,args) 
{ 

//if you are using dataset the data will be of type byte array byte[] 
//assuming you have assign the binary data to byte array 

byte[] data; 
Response.Clear(); //clear buffer 
Response.ContentType = "image/gif"; //should be the MIME type of the document see http://www.w3schools.com/media/media_mimeref.asp for the complete list 
Response.AddHeader("content-disposition", "attachment;filename=" + yourfilename); //tell the browser the file is to be downloaded 
Response.BinaryWrite(data); 
Response.End(); 

}