Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

upload file

  Asked By: Holly    Date: Apr 23    Category: Asp.net    Views: 942
  

how can i know what is the extension of an upload file.
i want to restrict EXE and maybe other files from uploading.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Adanalie Garcia     Answered On: Apr 23

im making file upload  user control.
one of the control properties is the allowed formats:
myUpload.allowedFormats = "gif|jpg|doc|txt|ppt|xls|htm|html";

now,i took the file  name and split the name and compare this string (split it
also) to the file extension.

it is not a good approch since a person can rename the file name to something
like:

myFile.jpg.exe

...

so what is the solution?if im taking myFile.postedFile.contentType

im getting in word application upload : application\msword...

i need the extension to compare to what the developer put in the property
(doc,htm...)

 
Answer #2    Answered By: Ginger Snyder     Answered On: Apr 23

This probably isn't the greatest solution, but you could determine your file
extentions in the following way:


string allowedFormats = "|gif|jpg|doc|txt|ppt|xls|htm|html|";
string x = "file.jpg";
Response.Write(x.Split(new char[] {'.'}, 2)[1] + "<br>");
if (allowedFormats.IndexOf("|" + x.Split(new char[] {'.'}, 2)[1] + "|") >
0 )
{
Response.Write("this extension is ok<br>");
}
else
{
Response.Write("this extension is NOT ok<br>");
}

string y = "file.jpg.ext";
Response.Write(y.Split(new char[] {'.'}, 2)[1] + "<br>");
if (allowedFormats.IndexOf("|" + y.Split(new char[] {'.'}, 2)[1] + "|") >
0 )
{
Response.Write("this extension is ok<br>");
}
else
{
Response.Write("this extension is NOT ok<br>");
}

The first filename, variable "x" - will print "this extension is ok" where
variable "y" will print "this extension is NOT ok"

 
Answer #3    Answered By: Jimmy Abp     Answered On: Apr 23

How about System.IO.Path.GetExtension() ?

look up the whole System.IO.Path "tree" on MSDN, you'll find lots of
goodies that should help in building an uploader.

 
Didn't find what you were looking for? Find more on upload file Or get search suggestion and latest updates.




Tagged: