Dear Friends,
I am using asp.net 2005. I have a problem in file upload html control. I want to filter the selected file format. Example:
In my file type area I want to select *.xls. It should list out XLS files only. Any one can help me it.
Unfortunately you cannot set a filter for the file browser dialog, but you could hook up a JavaScript that would trigger when the contents of the text box has changed and then validate the extension. That way you could inform the user that he/she has made
an invalid selection.
If this post was useful to you, please mark it as answer. Thank you!
but you could hook up a JavaScript that would trigger when the contents of the text box has changed and then validate the extension. That way you could inform the user that he/she has made an invalid selection.
Been there. Done that:
function checkFileExtension(elem) {
var filePath = elem.value;
if(filePath.indexOf('.') == -1)
return false;
var validExtensions = new Array();
var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
validExtensions[0] = 'jpg';
validExtensions[1] = 'jpeg';
validExtensions[2] = 'bmp';
validExtensions[3] = 'png';
validExtensions[4] = 'gif';
validExtensions[5] = 'tif';
validExtensions[6] = 'tiff';
validExtensions[7] = 'txt';
validExtensions[8] = 'doc';
validExtensions[9] = 'xls';
validExtensions[10] = 'pdf';
for(var i = 0; i < validExtensions.length; i++) {
if(ext == validExtensions[i])
return true;
}
alert('The file extension ' + ext.toUpperCase() + ' is not allowed!');
return false;
}
And to invoke this, add this line in your Page_Load:
I think it 's easy to realise it if you use C# (or VB,net) and .net fileupload control. you may define file types in arraylist "allowedExtensions".
string upload_Image(FileUpload fileupload,
string ImageSavedPath)
{
FileUpload fu = fileupload;
string imagepath =
"";
if (fileupload.HasFile)
kulanthaivel...
Member
20 Points
80 Posts
How to filter files in file upload HTML control
Sep 11, 2007 07:16 AM|LINK
Dear Friends,
I am using asp.net 2005. I have a problem in file upload html control. I want to filter the selected file format. Example:
In my file type area I want to select *.xls. It should list out XLS files only. Any one can help me it.
Thanks.
Filter files
johram
All-Star
28531 Points
3567 Posts
Re: How to filter files in file upload HTML control
Sep 11, 2007 08:56 AM|LINK
Unfortunately you cannot set a filter for the file browser dialog, but you could hook up a JavaScript that would trigger when the contents of the text box has changed and then validate the extension. That way you could inform the user that he/she has made an invalid selection.
JoshStodola
Star
13736 Points
3177 Posts
Re: How to filter files in file upload HTML control
Sep 11, 2007 01:57 PM|LINK
function checkFileExtension(elem) { var filePath = elem.value; if(filePath.indexOf('.') == -1) return false; var validExtensions = new Array(); var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase(); validExtensions[0] = 'jpg'; validExtensions[1] = 'jpeg'; validExtensions[2] = 'bmp'; validExtensions[3] = 'png'; validExtensions[4] = 'gif'; validExtensions[5] = 'tif'; validExtensions[6] = 'tiff'; validExtensions[7] = 'txt'; validExtensions[8] = 'doc'; validExtensions[9] = 'xls'; validExtensions[10] = 'pdf'; for(var i = 0; i < validExtensions.length; i++) { if(ext == validExtensions[i]) return true; } alert('The file extension ' + ext.toUpperCase() + ' is not allowed!'); return false; }And to invoke this, add this line in your Page_Load:
Hope this helps! Please mark the helpful post(s) as Answer.
steven.ye
Member
123 Points
34 Posts
Re: How to filter files in file upload HTML control
Sep 12, 2007 12:46 AM|LINK
{
FileUpload fu = fileupload; string imagepath = ""; if (fileupload.HasFile){
string filepath = Server.MapPath(ImageSavedPath); String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };for (int i = 0; i < allowedExtensions.Length; i++){
if (fileExtension == allowedExtensions[i]){
try{
string s_newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() +DateTime.Now.Second.ToString() + fileExtension;fu.PostedFile.SaveAs(filepath + s_newfilename);
imagepath = ImageSavedPath + s_newfilename;
}
catch (Exception ex){
Response.Write("File could not be uploaded.");}
}
}
}
return imagepath;}
harikrishna....
Member
2 Points
1 Post
Re: How to filter files in file upload HTML control
Apr 23, 2008 02:34 PM|LINK
Thank U .
uniservice3
Participant
982 Points
209 Posts
Re: How to filter files in file upload HTML control
Jul 23, 2011 06:13 PM|LINK
maybe this is beter for fast response ;)
<asp:RegularExpressionValidator ID="rexp" runat="server" ControlToValidate="fupProduct" ErrorMessage="Only .gif, .jpg, .png, .tiff and .jpeg" ValidationExpression="(.*\.([Gg][Ii][Ff])|.*\.([Jj][Pp][Gg])|.*\.([Bb][Mm][Pp])|.*\.([pP][nN][gG])|.*\.([tT][iI][iI][fF])$)"></asp:RegularExpressionValidator>morefays
Participant
1139 Points
237 Posts
Re: How to filter files in file upload HTML control
Oct 17, 2011 11:41 AM|LINK
check this link it will helps you http://dotnetfarrukhabbas.blogspot.com/2011/08/fileupload-control-file-type-and-file.html
Filter files
~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~
dotnetfarrukhabbasblogspot