从 uploadify 获取 .ashx 中的内容类型

发布于 2024-10-02 13:49:45 字数 235 浏览 2 评论 0原文

我可以通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-10-09 13:49:45

我相信最有可能的内容类型是由浏览器设置的。无论如何,不​​同的浏览器可能会为不同的文件设置不同的内容类型 - 并且它们可能会回退到通用内容类型,例如任何二进制文件(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文