Skip to content
Loading
How to check a uploaded file type using asp.net with C#.net  
  •   
  •   
    1. protected void btnUpload_Click(object sender, EventArgs e)  
    2. {  
    3. object documentFormat = 8;  
    4. string randomName = DateTime.Now.Ticks.ToString();  
    5. object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";  
    6. string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";  
    7. object fileSavePath = Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName);  
    8. string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);  
    9. //If Directory not present, create it.  
    10. if (!Directory.Exists(Server.MapPath("~/Temp/")))  
    11. {  
    12. Directory.CreateDirectory(Server.MapPath("~/Temp/"));  
    13. }  
    14. //Upload the word document and save to Temp folder.  
    15. FileUpload1.PostedFile.SaveAs(fileSavePath.ToString());  
    16. //Open the word document in background.  
    17. _Application applicationclass = new Application();  
    18. applicationclass.Documents.Open(ref fileSavePath);  
    19. applicationclass.Visible = false;  
    20. Document document = applicationclass.ActiveDocument;  
    21. //Save the word document as HTML file.  
    22. document.SaveAs(ref htmlFilePath, ref documentFormat);  
    23. //Close the word document.  
    24. document.Close();  
    25. //Read the saved Html File.  
    26. string wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());  
    27. //Loop and replace the Image Path.  
    28. foreach (Match match in Regex.Matches(wordHTML, "", RegexOptions.IgnoreCase))  
    29. {  
    30. wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);  
    31. }  
    32. dvWord.InnerHtml = wordHTML;  
    33. }  
    Can anyone guide me here.
     
    Thank you in advance!!

    4 Replies

    Know the answer? Post it — somebody with the same question will find it here.