}
else
{
FileUpload1.Focus();
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Max file size is 1 m.b');</script>");
}
}
else
{
FileUpload1.Focus();
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Only gif or jpg are allowed');</script>");
}
}
else
{
//-- if person not has image set default for him
}
Or try this using validation to validate upload file extensions to allow users to upload Gif or Jpeg images only:
try this example to allow user to upload file with Doc extension
how can i make fileupload control to only upload image files?
thank you
You can do with javascript
<head>
<script type ="text/javascript">
var validFiles=["bmp","gif","png","jpg","jpeg"];
function CheckExt(obj)
{
var source=obj.value;
var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
for (var i=0; i<validFiles.length; i++)
{
if (validFiles[i]==ext)
break;
}
if (i>=validFiles.length)
{
alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+validFiles.join(", "));
}
}
</script>
</head>
<body>
<asp:FileUpload ID="FileUpload1" runat="server" onchange ="CheckExt(this)" />
</body>
One more that checks file extension when upload is clicked
<head>
<script type ="text/javascript">
var validFiles=["bmp","gif","png","jpg","jpeg"];
function OnUpload()
{
var obj = document.getElementById("<%=FileUpload1.ClientID%>");
var source=obj.value;
var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
for (var i=0; i<validFiles.length; i++)
{
if (validFiles[i]==ext)
break;
}
if (i>=validFiles.length)
{
alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+validFiles.join(", "));
return false;
}
return true;
}
</script>
</head>
<body>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" OnClientClick ="return OnUpload()" />
</body>
aliusmankhan
Member
494 Points
435 Posts
Fileupload Control : Allow only for image files
Apr 16, 2009 06:04 AM|LINK
dear friends
how can i make fileupload control to only upload image files?
thank you
MSSQL Server 2008/2005/2k
Suprotim Aga...
Star
14263 Points
1973 Posts
MVP
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 06:35 AM|LINK
Here's a prototype code that you can use
if (FileUpload1.HasFile)
{
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (extension == ".jpg")
{
FileUpload1.SaveAs("yourpath" + FileUpload1.FileName);
}
else
{
Response.Write("Only .Jpg allowed");
}
}
Another thread which discusses various solutions http://forums.asp.net/p/1051895/2171502.aspx
ASP.NET with jQuery Articles | ASP.NET Tips and Tricks
venkatu2005
All-Star
32477 Points
6738 Posts
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 06:35 AM|LINK
Check this
http://venkat-dotnetsamples.blogspot.com/
Thanks.
shawpnendu
Contributor
4275 Points
749 Posts
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 06:36 AM|LINK
Hi,
You can check the uploadable format by:
if (file.ContentType.IndexOf(".....") >= 0)// response write to finout the string for image.
MCTS
http://shawpnendu.blogspot.com
mohd786hussain
Contributor
4331 Points
878 Posts
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 06:44 AM|LINK
Hi,
for alowing only image in file control you can use the following RegularExpressionValidator
<asp:RegularExpressionValidator runat="server" ErrorMessage="Only picture files are allowed!" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.gif|.GIF|.jpg|.JPG|.jpeg|.JPEG)$" ControlToValidate="fileUpload" Display="None"></asp:RegularExpressionValidator>
Or you can have a look at the link below :
http://forums.asp.net/t/1372463.aspx
Mohammad Hussain
Web design, Logo design & Asp.net development
yasserzaid
Star
14001 Points
2597 Posts
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 06:46 AM|LINK
Hi
try this to save uploaded file in Folder and rename file uploaded and validate uploaded file extension
in this example i upload files in Car_Image folder in my website
check this example with server side :
if (FileUpload1.HasFile == true && FileUpload1.PostedFile != null)
{
int _size = 1024;// equal 1 mb
string _fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
if (_fileExt.ToLower() == ".gif" || _fileExt.ToLower() == ".jpg" )
{
if (FileUpload1.PostedFile.ContentLength <= _size)
{
string CarFile = Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(Request.PhysicalApplicationPath + "Car_Image\\" + CarFile);
}
else
{
FileUpload1.Focus();
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Max file size is 1 m.b');</script>");
}
}
else
{
FileUpload1.Focus();
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('Only gif or jpg are allowed');</script>");
}
}
else
{
//-- if person not has image set default for him
}
Or try this using validation to validate upload file extensions to allow users to upload Gif or Jpeg images only:
try this example to allow user to upload file with Doc extension
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Only gif file allowed!"
ControlToValidate="ctlFileUpload"
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w](.)*))+(\.gif|\.GIF)$"
Width="510px" CssClass="Validator"></asp:RegularExpressionValidator>
//-----or
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Only gif file allowed!"
ControlToValidate="ctlFileUpload"
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.gif|.GIF)$"
Width="510px" CssClass="Validator"></asp:RegularExpressionValidator>
//-------or
<asp:RegularExpressionValidator ID="imgValidator" runat="server" ControlToValidate="imgUpload"
ErrorMessage="*gif or jpeg files only" ValidationExpression="^.+\.(([jJ][pP][eE]?[gG])|([gG][iI][fF]))$" />
or try this to allow user to upload doc file
<asp:RegularExpressionValidator ID="FileUpLoadValidator" runat="server" ErrorMessage="Only gif files!"
ValidationExpression=".*(\.gif|\.GIF)$" ControlToValidate="FileUpload1" Display="None">
Hope it helps you
you can also check this post
http://forums.asp.net/t/1405621.aspx
Good Luck
shahed.kazi
All-Star
17839 Points
3600 Posts
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 06:49 AM|LINK
bool fileOK = false;
if (fu.HasFile) // fu refers to FileUpload control.
{
String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();
String[] allowedExtensions = { ".jpg", ".gif", ".png" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
if (fileOk == true) { fu.SaveAs("path" + fu.FileName); }
}
}
.NET World |Captcha Control
mudassarkhan
All-Star
77546 Points
13188 Posts
MVP
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 07:11 AM|LINK
You can do with javascript
<head> <script type ="text/javascript"> var validFiles=["bmp","gif","png","jpg","jpeg"]; function CheckExt(obj) { var source=obj.value; var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase(); for (var i=0; i<validFiles.length; i++) { if (validFiles[i]==ext) break; } if (i>=validFiles.length) { alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+validFiles.join(", ")); } } </script> </head> <body> <asp:FileUpload ID="FileUpload1" runat="server" onchange ="CheckExt(this)" /> </body>Contact me
TUFAN01MAN
Participant
1192 Points
290 Posts
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 08:49 AM|LINK
Hi,
Basicly,
Dim yourFilename as string
yourFilename= System.IO.Path.GetExtension(YourControlToUpload.Filename such as fileupload)
and use if statememnt to check if it is ".jpg"
Best Regards ...
mudassarkhan
All-Star
77546 Points
13188 Posts
MVP
Re: Fileupload Control : Allow only for image files
Apr 16, 2009 08:57 AM|LINK
One more that checks file extension when upload is clicked
<head> <script type ="text/javascript"> var validFiles=["bmp","gif","png","jpg","jpeg"]; function OnUpload() { var obj = document.getElementById("<%=FileUpload1.ClientID%>"); var source=obj.value; var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase(); for (var i=0; i<validFiles.length; i++) { if (validFiles[i]==ext) break; } if (i>=validFiles.length) { alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+validFiles.join(", ")); return false; } return true; } </script> </head> <body> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" OnClientClick ="return OnUpload()" /> </body>Contact me