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.";
}
Đ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
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ó.)) –
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