TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Mas
NA
478
71.1k
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.
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 not present, create it.
if
(!Directory.Exists(Server.MapPath(
"~/Temp/"
)))
{
Directory.CreateDirectory(Server.MapPath(
"~/Temp/"
));
}
//Upload the word document and save to Temp folder.
FileUpload1.PostedFile.SaveAs(fileSavePath.ToString());
//Open the word document in background.
_Application applicationclass =
new
Application();
applicationclass.Documents.Open(
ref
fileSavePath);
applicationclass.Visible =
false
;
Document document = applicationclass.ActiveDocument;
//Save the word document as HTML file.
document.SaveAs(
ref
htmlFilePath,
ref
documentFormat);
//Close the word document.
document.Close();
//Read the saved Html File.
string
wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());
//Loop and replace the Image Path.
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!!
Reply
Answers (
4
)
how can i create mapping of two objects
how to convert IEnumerable<string> to byte[] in c#?