0

我有一个小的 asp.net 应用程序,它允许用户上传附件,并通过一个 asp:linkbutton 从另一个屏幕下载它们。在 IE10 中,当我单击链接按钮下载上传的文件时,IR 显示打开、保存、取消对话框,但它显示 URL 代替文件名。在 safari 和 firefox 中,这不会发生。它快把我逼疯了。我已经尝试了所有类型的标题/内容类型、内容配置组合,但都没有成功。下面是我在 firefox 和 safari 中工作的代码片段——它只是将二进制文件写入响应。

Dim bytes() As Byte = CType(dt.Rows(0)("UploadedFiles"), Byte())
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.AddHeader("Content-Disposition", "attachment;filename=" & Chr(34) & 
dt.Rows(0)    ("FileName").ToString & Chr(34))
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = dt.Rows(0)("ContentType").ToString()
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
Response.End()
4

1 回答 1

0

我就是这样做的,它在 IE8 中工作,抱歉我没有安装 IE10 来测试:

// myFile = an object containing information about the file being served
Response.Clear()
Response.Cache.SetExpires(DateTime.Now.AddMonths(1))
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.ContentType = myFile.contentType
Response.AddHeader("Content-Disposition",  "attachment; filename=" & myFile.targetFilename)
Response.BinaryWrite(bytes) // your code
//Response.WriteFile(myFile.sourceFilename, True) // my code
Response.End()

您可能不需要缓存的东西。

我之所以使用Response.WriteFile,是因为我将文件存储在文件系统上,而不是存储在数据库/字节数组中。您可以直接拨打BinaryWrite.

唯一的其他区别是 Content-Disposition 标头中文件名周围的双引号。我知道rfc2616显示它们,但我的代码没有它们也能工作。

于 2013-08-08T18:34:12.943 回答