Hello Members,
Hope you are doing good!!
How can we check the uploaded file is PDF / doc or docx.
- function ValidateFileUpload(Source, args) {
- var fuData = document.getElementById('<%= fileUpload.ClientID %>');
- var FileUploadPath = fuData.value;
- if (FileUploadPath == '') {
- args.IsValid = false;
- }
- else {
- var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
- if (Extension == "doc" || Extension == "docx" ) {
- args.IsValid = true;
- }
- if (Extension == "pdf"){
- args.IsValid = true;
- }
- else {
- args.IsValid = false
- }
- }
If it is PDF It needs to fire
- <body>
- <form id="form1" runat="server">
- <iframe id="myFrame" style="display:none" width="600" height="300"></iframe>
- <input type="button" value="Submit" onclick = "openPdf()"/>
- <script type="text/javascript">
- function openPdf()
- {
- var omyFrame = document.getElementById("myFrame");
- omyFrame.style.display="block";
- omyFrame.src = "http://www.africau.edu/images/default/sample.pdf";
- }
- </script>
- <div>
- <asp:TextBox ID="searchTxt"
- runat="server"
- Width="300px"
- Height="25px"
- Font-Size="Medium"></asp:TextBox>
- <input type="button" value="Search" onclick="return highlight(text) /*searchPrompt('search text', 'dvWord', false, true, 'red', 'orange')*/" />
- </div>
- </form>
- </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;'>";
- highlightEndTag = "</font>";
- }
- var newText = "";
- var i = -1;
- var lcSearchTerm = searchTerm.toLowerCase();
- var lcDivText = DivText.toLowerCase();
- while (DivText.length > 0) {
- i = lcDivText.indexOf(lcSearchTerm, i + 1);
- if (i < 0) {
- newText += DivText;
- DivText = "";
- } else {
- if (DivText.lastIndexOf(">", i) >= DivText.lastIndexOf("<", i)) {
- if (lcDivText.lastIndexOf("/script>", i) >= lcDivText.lastIndexOf("<script", i)) {
- newText += DivText.substring(0, i) + highlightStartTag + DivText.substr(i, searchTerm.length) + highlightEndTag;
- DivText = DivText.substr(i + searchTerm.length);
- lcDivText = DivText.toLowerCase();
- i = -1;
- }
- }
- }
- }
- return newText;
- }
- function highlightSearchTerms(searchText, divId, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) {
- debugger;
- if (treatAsPhrase) {
- searchArray = [searchText];
- } else {
- searchArray = searchText.split(" ");
- }
- var div=document.getElementById(divId);
- if (!div || typeof (div.innerHTML) == "undefined") {
- if (warnOnFailure) {
- alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
- }
- return false;
- }
- var DivText = div.innerHTML;
- for (var i = 0; i < searchArray.length; i++) {
- DivText = doHighlight(DivText, searchArray[i], highlightStartTag, highlightEndTag);
- }
- div.innerHTML = DivText;
- return true;
- }
- function searchPrompt(defaultSearchText, divId, isPrompt, treatAsPhrase, textColor, bgColor) {
- debugger;
- if ((!textColor) || (!bgColor)) {
- highlightStartTag = "";
- highlightEndTag = "";
- } else {
- highlightStartTag = "<font style='color:" + textColor + "; background-color:" + bgColor + ";'>";
- highlightEndTag = "</font>";
- }
- return highlightSearchTerms(searchTxt.value, divId, false, true, highlightStartTag, highlightEndTag);
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:FileUpload ID="FileUpload1" runat="server" />
- <asp:Button ID="btnUpload" runat="server" Text="submit" OnClick="btnUpload_Click" />
- <div></div>
- </div>
- <div>
- <asp:TextBox ID="searchTxt"
- runat="server"
- Width="300px"
- Height="25px"
- Font-Size="Medium"></asp:TextBox>
- <input type="button" value="Search" onclick="return searchPrompt('search text', 'dvWord', false, true, 'red', 'orange')" />
- </div>
- <div id="dvWord" runat="server"></div>
- </form>
- </body>
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- object documentFormat = 8;
- string randomName = DateTime.Now.Ticks.ToString();
- object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
- string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
- object fileSavePath = Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
- string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);
-
- if (!Directory.Exists(Server.MapPath("~/Temp/")))
- {
- Directory.CreateDirectory(Server.MapPath("~/Temp/"));
- }
-
- FileUpload1.PostedFile.SaveAs(fileSavePath.ToString());
-
- _Application applicationclass = new Application();
- applicationclass.Documents.Open(ref fileSavePath);
- applicationclass.Visible = false;
- Document document = applicationclass.ActiveDocument;
-
- document.SaveAs(ref htmlFilePath, ref documentFormat);
-
- document.Close();
-
- string wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());
-
- foreach (Match match in Regex.Matches(wordHTML, "<v:imagedata.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase))
- {
- wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);
- }
- dvWord.InnerHtml = wordHTML;
- }
Can anyone guide me here.
Thank you in advance!!