Mas

Mas

  • NA
  • 473
  • 64.9k

How to check a uploaded file type using asp.net with C#.net

Jan 21 2020 11:07 AM
Hello Members,
 
Hope you are doing good!!
 
How can we check the uploaded file is PDF / doc or docx.
  1. function ValidateFileUpload(Source, args) {  
  2. var fuData = document.getElementById('<%= fileUpload.ClientID %>');  
  3. var FileUploadPath = fuData.value;  
  4. if (FileUploadPath == '') {  
  5. args.IsValid = false;  
  6. }  
  7. else {  
  8. var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();  
  9. if (Extension == "doc" || Extension == "docx" ) {  
  10. args.IsValid = true;  
  11. }  
  12. if (Extension == "pdf"){  
  13. args.IsValid = true;  
  14. }  
  15. else {  
  16. args.IsValid = false  
  17. }  
  18. }  
If it is PDF It needs to fire
  1. <body>  
  2. <form id="form1" runat="server">  
  3. <iframe id="myFrame" style="display:none" width="600" height="300"></iframe>  
  4. <input type="button" value="Submit" onclick = "openPdf()"/>  
  5. <script type="text/javascript">  
  6. function openPdf()  
  7. {  
  8. var omyFrame = document.getElementById("myFrame");  
  9. omyFrame.style.display="block";  
  10. omyFrame.src = "http://www.africau.edu/images/default/sample.pdf";  
  11. }  
  12. </script>  
  13. <div>  
  14. <asp:TextBox ID="searchTxt"  
  15. runat="server"  
  16. Width="300px"  
  17. Height="25px"  
  18. Font-Size="Medium"></asp:TextBox>  
  19. <input type="button" value="Search" onclick="return highlight(text) /*searchPrompt('search text', 'dvWord', false, true, 'red', 'orange')*/" />  
  20. </div>  
  21. </form>  
  22. </body>  
If it is Doc or DOCX need to fire below doc
<script type="text/javascript">
function doHighlight(DivText, searchTerm, highlightStartTag, highlightEndTag) {
if ((!highlightStartTag) || (!highlightEndTag)) {
highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
  1. highlightEndTag = "</font>";  
  2. }  
  3. var newText = "";  
  4. var i = -1;  
  5. var lcSearchTerm = searchTerm.toLowerCase();  
  6. var lcDivText = DivText.toLowerCase();  
  7. while (DivText.length > 0) {  
  8. i = lcDivText.indexOf(lcSearchTerm, i + 1);  
  9. if (i < 0) {  
  10. newText += DivText;  
  11. DivText = "";  
  12. else {  
  13. if (DivText.lastIndexOf(">", i) >= DivText.lastIndexOf("<", i)) {  
  14. if (lcDivText.lastIndexOf("/script>", i) >= lcDivText.lastIndexOf("<script", i)) {  
  15. newText += DivText.substring(0, i) + highlightStartTag + DivText.substr(i, searchTerm.length) + highlightEndTag;  
  16. DivText = DivText.substr(i + searchTerm.length);  
  17. lcDivText = DivText.toLowerCase();  
  18. i = -1;  
  19. }  
  20. }  
  21. }  
  22. }  
  23. return newText;  
  24. }  
  25. function highlightSearchTerms(searchText, divId, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) {  
  26. debugger;  
  27. if (treatAsPhrase) {  
  28. searchArray = [searchText];  
  29. else {  
  30. searchArray = searchText.split(" ");  
  31. }  
  32. var div=document.getElementById(divId);  
  33. if (!div || typeof (div.innerHTML) == "undefined") {  
  34. if (warnOnFailure) {  
  35. alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");  
  36. }  
  37. return false;  
  38. }  
  39. var DivText = div.innerHTML;  
  40. for (var i = 0; i < searchArray.length; i++) {  
  41. DivText = doHighlight(DivText, searchArray[i], highlightStartTag, highlightEndTag);  
  42. }  
  43. div.innerHTML = DivText;  
  44. return true;  
  45. }  
  46. function searchPrompt(defaultSearchText, divId, isPrompt, treatAsPhrase, textColor, bgColor) {  
  47. debugger;  
  48. if ((!textColor) || (!bgColor)) {  
  49. highlightStartTag = "";  
  50. highlightEndTag = "";  
  51. else {  
  52. highlightStartTag = "<font style='color:" + textColor + "; background-color:" + bgColor + ";'>";  
  53. highlightEndTag = "</font>";  
  54. }  
  55. return highlightSearchTerms(searchTxt.value, divId, falsetrue, highlightStartTag, highlightEndTag);  
  56. }  
  57. </script>  
  58. </head>  
  59. <body>  
  60. <form id="form1" runat="server">  
  61. <div>  
  62. <asp:FileUpload ID="FileUpload1" runat="server" />  
  63. <asp:Button ID="btnUpload" runat="server" Text="submit" OnClick="btnUpload_Click" />  
  64. <div></div>  
  65. </div>  
  66. <div>  
  67. <asp:TextBox ID="searchTxt"  
  68. runat="server"  
  69. Width="300px"  
  70. Height="25px"  
  71. Font-Size="Medium"></asp:TextBox>  
  72. <input type="button" value="Search" onclick="return searchPrompt('search text', 'dvWord', false, true, 'red', 'orange')" />  
  73. </div>  
  74. <div id="dvWord" runat="server"></div>  
  75. </form>  
  76. </body>  
  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, "<v:imagedata.+?src=[\"'](.+?)[\"'].*?>", 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!!

Answers (4)