2011-09-12 64 views
7

我通常不使用.NET或C#或Microsoft服務器上的任何事情。我通常是LINUX LAMP Developer,所以請和我一起裸照。ASP.NET,C#,IIS,MIME類型,文件上傳條件

基本上,我有網站上的文件上傳網絡的形式,它需要只接受特定格式(或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."; 
    } 
+1

我唯一能想到的就是IIS沒有爲DOCX配置的MIME類型(不是它應該真的需要上載,但它可能有軸承),您是否檢查過一個擴展的設置? – dougajmcdonald

+1

查看[Fiddler](http://www.fiddler2.com/fiddler2/),這可能會幫助您確定究竟是什麼MIME字符串被推上電線(儘管我認爲它應該是「application/msword」你有。)) –

+2

當你上傳.docx文件時,FileUpload1.PostedFile.ContentType的值是多少?或者它沒有那麼遠? – Town

回答

2

this answer,這點你this page

DOCX Mime類型:

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

編輯: 對不起,沒有看到它在列表中。如果你想允許DOCX,爲什麼不檢查「.docx」作爲擴展名。

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

他已經處理那種情況。看看他的代碼(OR條件)。 – Icarus

+1

他已經在檢查MIME類型。 – Town

+0

大家好,謝謝你的幫助。您在上面看到的代碼接受WORD(.DOC),.PDFS和其中列出的所有其他內容,除了.DOCX ...仍然不確定發生了什麼事情。 –

1

您應該閱讀擴展名,而不是檢查MIME類型。 MIME類型由瀏覽器設置,並且可能在計算機之間不同。這不是他們的目的。另外,無論你來自哪個背景,如果你有一個如果這樣的陳述,你應該至少感到一絲羞恥。

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


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