简体   繁体   English

在ASP.NET中检索上传的文件

[英]Retrieve uploaded file in ASP.NET

I am using ASP.NET and C#. 我正在使用ASP.NET和C#。

I have made a document uploading page, In which use can upload their document. 我做了一个文件上传页面,在其中可以使用其文件上传。 I am saving three fields in the database, 我在数据库中保存了三个字段,

  1. Document Name [NVarchar] 文件名称[NVarchar]
  2. File [Image] 文件[图像]
  3. DocumentType [NVarchar] DocumentType [NVarchar]

Now, I am able to add records in the database, successfully. 现在,我能够成功地在数据库中添加记录。 Now I want to show it in the gridview, like DocumentName, DocumentType and a link to download the file. 现在,我想在gridview中显示它,例如DocumentName,DocumentType以及下载文件的链接。

I have tried by retrieving the records and assigning them to the gridview but I am getting only two columns. 我尝试通过检索记录并将它们分配给gridview来进行尝试,但是我仅得到两列。

You have to create a download handler that serves the actual file. 您必须创建一个提供实际文件的下载处理程序。

The handler can retrieve the Binary from the table and write is directly to the output stream. 处理程序可以从表中检索Binary并将其直接写入输出流。

The grid view would then point to this handler 然后,网格视图将指向此处理程序

Since you are storing files in DB you will have to write code to read the file data, add appropriate header and do response.write 由于将文件存储在DB中,因此必须编写代码以读取文件数据,添加适当的标头并执行response.write

eg 例如

on your LinkButton click handler the code will be something like 在您的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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM