【发布时间】:2011-11-15 11:53:08
【问题描述】:
我在网站上有一个文件上传网络表单,它只需要接受某些格式(或 MIME 类型)...
以下代码运行良好,除了它不会将 .DOCX 文件上传到服务器!那是唯一不起作用的文件类型...我仔细检查了每一行代码,甚至进入了 IIS 管理器以确保 .DOCX MIME 类型被继承,它们是...
有人知道为什么 .DOCX 文件不会像其他文件类型那样上传到服务器吗?
代码片段:
string savePath = "D:\\HIDDEN PATH HERE";
string fileMsg;
// Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Check to see that the content type is proper and allowed.
// DOC: application/doc, appl/text, application/vnd.msword, application/vnd.ms-word, application/winword, application/word, application/x-msw6, application/x-msword
if (
FileUpload1.PostedFile.ContentType == "text/rtf" ||
FileUpload1.PostedFile.ContentType == "application/doc" ||
FileUpload1.PostedFile.ContentType == "appl/text" ||
FileUpload1.PostedFile.ContentType == "application/vnd.msword" ||
FileUpload1.PostedFile.ContentType == "application/vnd.ms-word" ||
FileUpload1.PostedFile.ContentType == "application/winword" ||
FileUpload1.PostedFile.ContentType == "application/word" ||
FileUpload1.PostedFile.ContentType == "application/msword" ||
FileUpload1.PostedFile.ContentType == "application/x-msw6" ||
FileUpload1.PostedFile.ContentType == "application/x-msword" ||
FileUpload1.PostedFile.ContentType == "application/pdf" ||
FileUpload1.PostedFile.ContentType == "application/x-pdf" ||
FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName;
// Append the name of the file to upload to the path.
savePath += strnow + fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user of the name of the file
// was saved under.
//fileMsg = "Your file was saved as " + fileName;
fileMsg = "";
}
else
{
fileMsg = "Your file was not an accepted format. Please use PDF, RTF or DOC formats.";
}
【问题讨论】:
-
我唯一能想到的可能是 IIS 没有为 DOCX 配置 mime 类型(不是说它真的需要它来上传,但也许它有影响),你检查过吗扩展程序有一个设置吗?
-
查看Fiddler,这可能会帮助您准确确定正在向上推送的 MIME 字符串(尽管我认为它应该是
application/msword(您有.)) -
上传.docx时
FileUpload1.PostedFile.ContentType的值是多少?还是没有那么远? -
您是否也在尝试解决覆盖问题?
-
@Mr.失望:我认为对于 docx,它应该是
application/vnd.openxmlformats-officedocument.wordprocessingml.document,但他也有——我只是想你可能想知道。 :)
标签: c# asp.net file-upload mime-types