问题描述
我有一个asp.net应用程序,用户可以将文件上传到我们的数据库。
有时当他们上传文件时, content-type
被设置为application / octet-stream
这是一个二进制文件。
当我询问用户时,他们说他们上传了他们说的是 .tif
文件。不知怎的,上传控件将其设置为application / octet-stream
。 当我从我的电脑上传相同的 .tif
文件时,它会上传正确的内容类型(应用/八位字节流
)。
我使用下面的代码来获取文件扩展名
fileExtension = filUpload.PostedFile.FileName.Substring(filUpload.PostedFile.FileName.LastIndexOf(。)+ 1)
有时它返回文件扩展名为c:\documen
或j:\ testing$ c $我知道Windows不允许在文件名中使用特殊字符。
您根本无法依靠浏览器提交可用的MIME媒体类型。客户端机器可能没有为特定的文件类型设置任何媒体类型信息(在这里可能是TIFF的情况),或者根本不支持发送媒体类型,或者可能有错误(因为已经存在在过去与IE)。
你也不能依靠浏览器提交一个可用的文件扩展名。客户机可能不使用文件扩展名来确定文件的类型。 (事实上,Mac和现代Linux使用多种机制来确定类型,所以任何文件扩展名都可能是误导性的,如果有的话。)
对于这个问题,甚至不能依靠浏览器提交一个可用的文件名!不是每个操作系统都使用反斜线字符和点来表示目录和扩展分隔符;提交的文件名实际上是一个不透明的字符串,你可以用它来猜测一些常见的情况,但你不能认为是确定性的。
所以唯一合理的方法确定上传文件的类型是:
-
询问用户显示他们上传的是什么类型。
尝试猜测媒体类型和结尾文件名可能是什么类型,然后回头询问用户是什么类型。 -
如果你想允许的类型都是带有可嗅探标题的类型(如TIFF和大多数其他图像格式),你可以通过查看文件内容来计算出类型。
I have an asp.net application where the users can upload files to our database.
Sometimes when they upload the files, the content-type
is set as "application/octet-stream"
which is a binary file.
When I ask the user, they say that they uploaded they say it was a .tif
file. Somehow the upload control sets it as "application/octet-stream"
.
When I upload the same .tif
file from my computer it uploads with the correct content type (application/octet-stream
).
I am using the following code to get the file extension
fileExtension = filUpload.PostedFile.FileName.Substring(filUpload.PostedFile.FileName.LastIndexOf(".") + 1)
sometimes it returns the file extension as "c:\documen"
or "j:\testing"
etc. I know that windows doesn't allow special characters in the filename.
You simply can't rely on the browser to submit a usable MIME media type. The client machine may not have any media type information set up for a particular filetype (which is likely the case for TIFF here), or it may not support sending media types at all, or there may be bugs in it (as there have been in the past with IE).
You also can't rely on the browser to submit a usable filename extension. The client machine may not use file extensions to determine the type of the file. (Indeed, Macs and modern Linux use multiple mechanisms to determine type, so any filename extension may be misleading, if one is present at all.)
For that matter, you can't even rely on the browser to submit a usable filename in the first place! Not every OS uses backslash character and dot for directory and extension separators; the submitted filename is effectively an opaque string which you can use for guessing some of the common cases, but you can't consider to be definitive.
So the only reasonable ways to determine the type of an uploaded file are:
Ask the user explicitly what type they're uploading.
Try to guess what type it might be from media type and trailing filename, falling back to asking the user what type it is.
If the types you want to allow are all ones with sniffable headers (as TIFF and most other image formats are), you can work out the type by looking at the contents of the file.
这篇关于上传文件时获取不正确的文件扩展名和内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!