2011-09-12 12 views
7

मैं आमतौर पर में नेट या सी #, या कुछ भी उस बात के लिए किसी Microsoft सर्वर पर काम नहीं करते। मैं आमतौर पर एक LINUX लैंप डेवलपर हूँ, तो कृपया मेरे साथ बेकार है।ASP.NET, सी #, आईआईएस, माइम प्रकार, फ़ाइल अपलोड करें सशर्त

असल में, मैंने वेबसाइट पर फाइल अपलोड वेब फ़ॉर्म दिया गया है और यह केवल कुछ स्वरूपों (या MIME प्रकार) को स्वीकार करने की जरूरत है ...

निम्नलिखित कोड पूरी तरह से काम कर रहा है, को छोड़कर, इसे अपलोड नहीं करता है सर्वर पर डॉएक्सएक्स फाइलें! यह एकमात्र फ़ाइल प्रकार है जो काम नहीं करता है ... मैंने कोड की प्रत्येक पंक्ति को दो बार चेक किया है और आईआईएस मैनेजर में भी यह सुनिश्चित करने के लिए प्राप्त किया है कि डीओएक्सएक्स एमआईएम प्रकार विरासत में थे, और वे ...

कोई भी क्या कोई विचार है कि क्यों। डीओएक्सएक्स फाइलें सर्वर पर अपलोड नहीं होंगी जैसे कि हर दूसरे फाइल टाइप करता है? कोड की

स्निपेट:

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

केवल बात मैं के बारे में सोच सकते हैं आईआईएस DOCX (नहीं यह वास्तव में अपलोड करने के लिए इसकी जरूरत चाहिए, लेकिन शायद यह असर है कि) के लिए कॉन्फ़िगर एक माइम प्रकार नहीं है शायद है, तो आप की जाँच की है कि वहाँ है विस्तार के लिए एक सेटअप? – dougajmcdonald

+1

देखें [फ़िडलर] (http://www.fiddler2.com/fiddler2/), इस मदद कर सकता है आप यह निर्धारित वास्तव में क्या माइम स्ट्रिंग तार को धक्का दिया जा रहा है (हालांकि मुझे लगता है कि यह हो _should_ 'आवेदन/msword' (जो आप कर सकते है।)) –

+2

जब आप .docx अपलोड 'FileUpload1.PostedFile.ContentType' का मूल्य क्या है? या यह अब तक नहीं मिलता है? – Town

उत्तर

2

this answer, जो आप this page के लिए अंक मिलते हैं।

DOCX माइम प्रकार:

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

संपादित करें: क्षमा करें, यह सूची में नहीं देखा। यदि आप डॉक्सएक्स को अनुमति देना चाहते हैं, तो एक्सटेंशन के रूप में ".docx" की जांच क्यों न करें।

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

वह पहले से ही हैंडलिंग कर रहा है उस मामले में। उसके कोड (या शर्तों) को देखो। – Icarus

+1

वह पहले से ही है कि MIME प्रकार के लिए जाँच कर रहा है। – Town

+0

हैलो सब, आपकी मदद के लिए धन्यवाद। जो कोड आप ऊपर देखते हैं वोर्ड (डीओसी), पीडीएफएस, और वहां सूचीबद्ध अन्य सभी चीज़ों को स्वीकार करता है, डीसीएक्सएक्स ... अभी भी सुनिश्चित नहीं है कि क्या हो रहा है .. –

1

आपको विस्तार पढ़ना चाहिए, माइम प्रकारों की जांच नहीं करना चाहिए। एमआईएमई प्रकार ब्राउज़र द्वारा सेट किए जाते हैं और कंप्यूटर के बीच अलग हो सकते हैं। यह उनका उद्देश्य नहीं है।

इसके अलावा, क्या पृष्ठभूमि आप से आते हैं, यदि आप एक अगर ऐसा बयान है, तो आप कम से कम शर्म की बात है की थोड़ी सी भी बिट महसूस करना चाहिए की परवाह किए बिना।

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


if(acceptedExtensions.Contains(Path.GetExtension(FileUpload1.PostedFile.Filename))) 
{ 
    AcceptFile(FileUpload1.PostedFile); 
} 
संबंधित मुद्दे