Asp.net在移动平台下载文件
我有一些文件存储在 SQL 数据库中。当用户访问具有给定 ID 的 url 时,BLOB 数据会通过以下方式从数据库检索到 Web 浏览器:
Byte[] myData = b.BlobContent;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", b.SizeInBytes.ToString());
Response.BinaryWrite(myData);
Response.End();
但是,iPhone (Safari) 无法下载 *.txt 和 *.doc 文件。它说:“Safari 无法下载此文件”。可以下载并查看 PDF(!)。
在 Android 上,无法下载任何文件。
这是因为 iPhone 和 Android 无法处理所有文件吗?或者我在响应中做错了什么。
I've some files stored in a SQL database. When a user visits a url with the given ID, the BLOB data is retrieved from the database to the webbrower via:
Byte[] myData = b.BlobContent;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", b.SizeInBytes.ToString());
Response.BinaryWrite(myData);
Response.End();
However, the iPhone (Safari) can't download a *.txt and *.doc file. It says: "Safari can't download this file". A pdf can(!) be downloaded and viewed.
On Android none of the files can be downloaded.
Is this because the iPhone and Android just can't handle all files? Or am I doing something wrong in the Response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
application/octent-stream 的内容类型不适合 .txt 和 .doc 文件。对于您想要使用“text/plain”的 .txt 和您想要“application/msword”的 .doc 文件,我建议您将 Content-Type 值存储在数据库中。这是一个很好的mime 类型参考。如果您通过 FileUpload 控件接受文件,PostedFile 对象应该告诉您内容类型。
编辑 1:此外,您不需要设置内容长度,因为 ASP.Net 会为您执行此操作。
The content type of application/octent-stream is not appropriate for .txt and .doc files. for .txt you want to use "text/plain" and .doc files you want "application/msword" I suggest that you store the Content-Type value in your database. Here is a good reference for mime types. If you are accepting the files through the FileUpload control the PostedFile object should tell you the contentn type.
EDIT 1: Also, you should not need to set the content-length, as ASP.Net will do this for you.