2011-09-12 2 views
7

저는 일반적으로 .NET이나 C# 또는 그와 관련해 Microsoft 서버의 어떤 것도 작동하지 않습니다. 저는 보통 LINUX LAMP 개발자입니다. 그러니 나와 함께 절을하십시오. 기본적으로ASP.NET, C#, IIS, MIME 유형, 파일 업로드 조건

,

다음 코드를 제외하고, 완벽하게 작동한다

, 그것은 업로드하지 않습니다 ... 나는 웹 사이트에 파일 업로드 웹 양식을 가지고 있고 그것은 특정 형식 (또는 MIME 타입)을 채택 할 필요가 .DOCX 파일을 서버에 저장하십시오! 그게 작동하지 않는 유일한 파일 형식입니다 ... 나는 코드의 모든 라인을 두 번 체크하고 심지어 .DOCX MIME 타입이 계승되었는지 확인하기 위해 IIS 관리자에 들어갔다. 그리고 그들은 ...

.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 page로 포인트 this answer을 참조하십시오.

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); 
}