从 uploadify 获取 .ashx 中的内容类型
我可以通过 uploadify + .ashx
上传我的文件,但问题是我总是得到 ContentType = application/octet-stream
假设我上传了一个图像,我希望返回我“image/pjpeg”,但无论我上传什么文件,它总是返回“application/octet-stream”。
请建议如何在 .ashx 中获取正确的 contentType
I able to upload my file through uploadify + .ashx
, but the problem is I always get ContentType = application/octet-stream
Lets say I upload an image, I expected to return me "image/pjpeg"
, but it always return "application/octet-stream"
no matter what file I uploaded.
Please advice how to get the correct contentType in .ashx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信最有可能的内容类型是由浏览器设置的。无论如何,不同的浏览器可能会为不同的文件设置不同的内容类型 - 并且它们可能会回退到通用内容类型,例如任何二进制文件(pdf、zip、doc、xls)的“application/octet-stream”。一种浏览器可能会将 docx 报告为“application/vnd.openxmlformats”,而其他浏览器将其报告为“”application/x-zip-compressed”,而另一种浏览器会将 docx 报告为“application/octet-stream”。但所有这些都是正确的,因为docx 是二进制文件,并且是压缩(zip)文件。
简而言之,我的建议是您不应依赖客户端发送的内容类型(超出一定程度,例如确定其是文本、html 还是二进制等),而是使用服务器。侧面嗅探逻辑来确定文件内容的类型。简单的嗅探可以基于文件扩展名,而更强大的实现将掠夺实际的文件内容,其中通常文件的前几个字节指示文件类型。
I believe that most probably content type is getting set by browser. Regardless, different browsers may set different content type for different files - and they may fall back to generic content type such as "application/octet-stream" for any binary file (pdf, zip, doc, xls). Its possible that one browser would report docx as "application/vnd.openxmlformats" while other as ""application/x-zip-compressed" and yet another as "application/octet-stream". And yet all of them are correct, because docx are binary file and are compressed (zip) files.
In short, my suggestion is that you should not rely on the content type sent by client (beyond certain extent such as deciding whether its text, html or binary etc) and rather use server side sniffing logic to determine type of file content. Simple sniffing can be based on file extension while more robust implementation will loot at actual file contents where typically first few bytes of file indicate the file type.