ASP.NET,C#,IIS,MIME类型,文件上传的先决条件, [英] ASP.NET, C#, IIS, MIME TYPES, FILE UPLOAD CONDITIONAL

查看:101
本文介绍了ASP.NET,C#,IIS,MIME类型,文件上传的先决条件,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不.NET或C#,或任何平时工作为此事Microsoft服务器上。我是一个LINUX LAMP开发者一般,所以请与我裸露。

基本上,我有网站上的文件上传网络的形式,它需要只接受特定格式(或MIME类型)...

以下code为正常使用,除非,它不.DOCX文件上传到服务器!这是唯一的文件类型不工作...我有双重检查code的每一行,甚至陷入IIS管理,以确保.DOCX MIME类型被继承,而且他们...

有没有人有任何想法,为什么.DOCX文件不会上传到服务器像所有其他文件类型呢?

code的片段:

 字符串savePath =D:\\\\隐蔽的小路HERE;
串fileMsg;//之前试图执行操作
//上的文件,确认文件上传
//控件包含一个文件。
如果(FileUpload1.HasFile)
{
    //请检查内容类型是正确的和允许的。
    // DOC:应用程序/文件,申请/文本,应用程序/ vnd.msword,应用程序/ vnd.ms字,应用/ WINWORD,应用/字,应用程序/ x-msw6,应用程序/ x-MSWORD
    如果(
        FileUpload1.PostedFile.ContentType ==文/ RTF||
        FileUpload1.PostedFile.ContentType ==应用程序/文件||
        FileUpload1.PostedFile.ContentType ==申请/文本||
        FileUpload1.PostedFile.ContentType ==应用程序/ vnd.msword||
        FileUpload1.PostedFile.ContentType ==应用程序/ vnd.ms字||
        FileUpload1.PostedFile.ContentType ==应用程序/ WINWORD||
        FileUpload1.PostedFile.ContentType ==应用程序/字||
        FileUpload1.PostedFile.ContentType ==应用程序/ msword||
        FileUpload1.PostedFile.ContentType ==应用程序/ x-msw6||
        FileUpload1.PostedFile.ContentType ==应用程序/ x-MSWORD||
        FileUpload1.PostedFile.ContentType ==应用程序/ PDF||
                        FileUpload1.PostedFile.ContentType ==应用程序/ x-PDF||
        FileUpload1.PostedFile.ContentType ==应用程序/ vnd.openxmlformats-officedocument.wordpro​​cessingml.document||
        FileUpload1.PostedFile.ContentType ==应用程序/ vnd.openxmlformats-officedocument.wordpro​​cessingml.template
        )
    {
        //获取文件的名称上传。
        字符串文件名= FileUpload1.FileName;        //追加该文件的名称来上传到的路径。
        savePath + = strnow +文件名;
        //调用SaveAs方法来保存
        //上传文件到指定的路径。
        //这个例子不执行所有
        //必要的错误检查。
        //如果具有相同名称的文件
        //已经存在于指定的路径,
        //上传的文件覆盖它。
        FileUpload1.SaveAs(savePath);        //通知的文件的名称的用户
        //被下保存。
        // fileMsg =您的文件保存为+文件名;
        fileMsg =;
    }
    其他
    {
        fileMsg =您的文件不是一个接受的格式,请使用PDF,RTF或DOC格式。
    }


解决方案

请参阅this回答,这点你此页面

DOCX Mime类型:

 应用程序/ vnd.openxmlformats-officedocument.wordpro​​cessingml.document

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

  || 。FileUpload1.PostedFile.FileName.ToLower()子串(FileUpload1.PostedFile.FileName.Length  -  4)==DOCX

I don't usually work in .NET or C#, or anything on a Microsoft server for that matter. I'm a LINUX LAMP Developer typically, so please bare with me.

Basically, I have a file upload web-form on the website and it needs to only accept certain formats (or MIME types)...

The following code is working PERFECTLY, EXCEPT, it does not upload .DOCX files to the server ! That is the only file type that does not work... I have double checked every line of code and have even gotten into the IIS manager to ensure .DOCX MIME types were inherited, and they were...

Does anyone have any idea why .DOCX files will not upload to the server like every other filetype does?

Snippet of code:

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."; 
    }

解决方案

See this answer, which points you to this page.

DOCX Mime Type:

application/vnd.openxmlformats-officedocument.wordprocessingml.document

EDIT: Sorry, didn't see it in the list. If you want to allow DOCX, why not just check for ".docx" as the extension.

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

这篇关于ASP.NET,C#,IIS,MIME类型,文件上传的先决条件,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆