【问题标题】:Retrieve uploaded file in ASP.NET在 ASP.NET 中检索上传的文件
【发布时间】:2010-08-05 10:25:55
【问题描述】:

我正在使用 ASP.NET 和 C#。

我做了一个文件上传页面,在里面可以上传自己的文件。我正在数据库中保存三个字段,

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

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

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

【问题讨论】:

  • 我认为您需要手动创建链接才能下载文件。我不认为 GridView 可以自动为您执行此操作。
  • 我创建了这个:​​ton>

标签: asp.net gridview file-upload


【解决方案1】:

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

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

然后网格视图将指向这个处理程序

【讨论】:

【解决方案2】:

由于您将文件存储在数据库中,因此您必须编写代码来读取文件数据,添加适当的标头并执行 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();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2011-03-30
    • 2020-05-24
    相关资源
    最近更新 更多