【问题标题】:ASP.NET, C#, IIS, MIME TYPES, FILE UPLOAD CONDITIONALASP.NET、C#、IIS、MIME 类型、文件上传条件
【发布时间】: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


【解决方案1】:

您应该阅读扩展程序,而不是检查 mime 类型。 MIME 类型由浏览器设置,并且在计算机之间可能不同。这不是他们的目的。

另外,无论你来自什么背景,如果你有这样的 if 语句,你至少应该感到一丝羞愧。

string[] acceptedExtensions = new string[] { ".docx", ".doc", ".txt", ".etc" };
// snip


if(acceptedExtensions.Contains(Path.GetExtension(FileUpload1.PostedFile.Filename))) 
{ 
    AcceptFile(FileUpload1.PostedFile);
}

【讨论】:

    【解决方案2】:

    查看this answer,它会将您指向this page

    DOCX 哑剧类型:

    application/vnd.openxmlformats-officedocument.wordprocessingml.document
    

    编辑: 抱歉,没有在列表中看到它。如果你想允许 DOCX,为什么不检查“.docx”作为扩展名。

    || FileUpload1.PostedFile.FileName.ToLower().Substring(FileUpload1.PostedFile.FileName.Length - 4) == "docx"
    

    【讨论】:

    • 他已经在处理那个案子了。查看他的代码(或条件)。
    • 大家好,感谢您的帮助。您在上面看到的代码接受 WORD (.DOC)、.PDFS 以及那里列出的所有其他内容,除了 .DOCX ......仍然不确定发生了什么......
    • 我选择只阅读扩展名...尽管我认为这不是最安全的方式。
    • 阅读扩展是正确的方法,克里斯。正如 OP 所发现的那样,Mime 类型对此并不可靠。
    • 旧帖子,但假设一下:如果我将 .exe 或 .zip 文件的本地扩展名更改为 .doc 会怎样?不得不同意克里斯对此的犹豫。
    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 2015-01-11
    • 2012-02-05
    • 2016-02-25
    相关资源
    最近更新 更多