Upload Files : Set Filter

  • Thread starter vishpala kadam via .NET 247
  • Start date
V

vishpala kadam via .NET 247

Hi,

I am using <input type="file"> to upload files in my asp.net application.
I want to set the filter so that user can view only .xls files once he/she clicks the Browse button. Is it possible? How?
 
B

b.Wurm

There is a html attribute where you can specify the filetypes you want to
allow, but the problem is, that most of the browsers (for example IE!)
ignores this attribute.
so you can't really handle this!
You have to check the file on the server after upload.

regards
b.wurm
 
A

Alex Smith

HI vishpala,
you can try with this type of code
OpenFileDialog fileopen = new OpenFileDialog();

fileopen.Title = "Select only Excel File ";

fileopen.Filter = "Excel files(*.xls)|*.xls";

void Browse_Click(object sender, EventArgs e)

{

fileopen.ShowDialog();

}



Alex.
 
H

hashimisayed

The easiest solution is to allow the user to select what ever file he
wants, but before you actually submit to the server, verify that he has
selected the correct file type. Here is a simple html page that only
allows uploads of certain file types.
<HTML>
<HEAD>
<TITLE>My Upload Page</TITLE></HEAD>
<BODY>
<script language="JavaScript">
function verifyFile()
{
if(document.fileUpForm.uploadedFile.value == "") {
alert("Must choose a file first!");
return false;
}
if(document.fileUpForm.uploadedFile.value.length < 5) {
alert("Invalid filename. Must contain proper extension.");
return false;
}
if(document.fileUpForm.uploadedFile.value.search(
/\.(doc|gif|jpg|jpeg|pdf|txt|xls)$/
) == -1) {
alert("Invalid filename extension.");
return false;
}
return true;
}
</script>
<H2>Upload File</H2>
<form name="fileUpForm" method="POST" enctype="multipart/form-data"
action="http://www.mycomp.com/upload/UploadHandler.aspx"
ID="Form1">
File to upload: <input name="uploadedFile" type="file" size="40"
ID="File1" /><p>
<input type="button" name="submitbutton" value="Submit"
onClick="if(verifyFile()){document.fileUpForm.submit();}"
ID="Button1" />
</form>
</BODY>
</HTML>

The page only allows uploads of files with the following extension:
doc, gif, jpg, jpeg, pdf, txt, and xls. You can modify the list to
allow your file types.

sayed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top