2011-09-12 34 views
7

Tôi thường không làm việc trong .NET hoặc C#, hoặc bất kỳ thứ gì trên máy chủ Microsoft cho vấn đề đó. Tôi là một nhà phát triển LAMP LINUX thường, vì vậy xin vui lòng trần với tôi.ASP.NET, C#, IIS, MIME TYPES, FILE UPLOAD CONDITIONAL

Về cơ bản, tôi có một tập tin tải lên web-form trên trang web và nó cần phải chỉ chấp nhận các định dạng nhất định (hoặc các loại MIME) ...

Các mã sau đây được làm việc hoàn hảo, TRỪ, nó không tải lên .DOCX tệp đến máy chủ! Đó là loại tệp duy nhất không hoạt động ... Tôi đã kiểm tra kỹ từng dòng mã và thậm chí đã nhận được vào trình quản lý IIS để đảm bảo các loại .DOCX MIME được thừa hưởng và chúng ...

Có ai không có bất kỳ ý tưởng tại sao các tệp .DOCX sẽ không tải lên máy chủ giống như mọi loại tệp khác không?

Snippet mã:

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

Điều duy nhất tôi có thể nghĩ là có lẽ IIS không có loại mime được cấu hình cho DOCX (không thực sự cần nó để tải lên, nhưng có lẽ nó có ổ), bạn đã kiểm tra xem có một thiết lập cho tiện ích mở rộng? – dougajmcdonald

+1

Kiểm tra [Fiddler] (http://www.fiddler2.com/fiddler2/), điều này có thể giúp bạn xác định chính xác chuỗi MIME nào đang được đẩy lên dây (mặc dù tôi nghĩ rằng nó _should_ là 'application/msword' (mà bạn có.)) –

+2

Giá trị của 'FileUpload1.PostedFile.ContentType' khi bạn tải lên .docx là gì? Hay nó không đi xa đến vậy? – Town

Trả lời

2

Xem this answer, mà chỉ bạn this page.

Loại DOCX Mime:

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

EDIT: Xin lỗi, không nhìn thấy nó trong danh sách. Nếu bạn muốn cho phép DOCX, tại sao không chỉ kiểm tra ".docx" làm phần mở rộng.

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

Ông đã xử lý trường hợp. Nhìn vào mã của anh ta (điều kiện HOẶC). – Icarus

+1

Anh ấy đã kiểm tra loại MIME đó. – Town

+0

Xin chào tất cả, cảm ơn sự giúp đỡ của bạn. Mã bạn nhìn thấy ở trên chấp nhận WORD (.DOC), .PDFS, và mọi thứ khác được liệt kê ở đó, EXCEPT .DOCX ... Vẫn không chắc chắn những gì đang xảy ra .. –

1

Bạn nên đọc phần mở rộng, không kiểm tra loại mime. Các loại MIME được thiết lập bởi trình duyệt và có thể khác nhau giữa các máy tính. Đây không phải là mục đích của họ.

Ngoài ra, bất kể bạn đến từ nền nào, nếu bạn có tuyên bố nếu như vậy, bạn sẽ cảm thấy ít nhất một chút xấu hổ.

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


if(acceptedExtensions.Contains(Path.GetExtension(FileUpload1.PostedFile.Filename))) 
{ 
    AcceptFile(FileUpload1.PostedFile); 
} 
Các vấn đề liên quan