Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to filter pics before into database by using fileupload control can u send me guide lines or any snippets
filtering of images types
Posted
Updated 7-Feb-12 19:09pm
v3
Comments
Uday P.Singh 8-Feb-12 1:07am    
not clear! what kind of filtration do you need?
kingsa 8-Feb-12 1:10am    
images of types i want jpeg,png,bmp only can i filter before inserting into data base

you can validate with RegEx See the Example below

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function validate() {
var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
//Regular Expression for fileupload control.
var reg = /^(([a-zA-Z])|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)$/;
if (uploadcontrol.length > 0)
{
//Checks with the control value.
if (reg.test(uploadcontrol))
{
return true;
}
else 
{
//If the condition not satisfied shows error message.
alert("Only .doc, docx files are allowed!");
return false;
}
}
} //End of function validate.
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
Restrict File Upload sample <br />
<asp:fileupload id="FileUpload1" runat="server" xmlns:asp="#unknown" />
<br />
<asp:button id="Button1" runat="server" text="Button" onclientclick="return validate();" xmlns:asp="#unknown" />
<br />
<asp:label id="Label1" runat="server" forecolor="Red" xmlns:asp="#unknown"></asp:label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Other Link Reference
http://blogs.msdn.com/b/asmitaw/archive/2008/09/18/validating-image-file-for-content-type-with-asp-net-fileupload-web-control.aspx[^]
http://mindstick.com/Articles/7d72a758-52bc-4242-922f-6b3914895d92/?Validating%20FileUpload%20contro[^]
http://forums.asp.net/p/1051895/2171502.aspx[^]


hope this will help you
dont forget to vote or accept solution
thanks
 
Share this answer
 
v2
Hi,

Try this if could help...

C#
// Filter template extention
private string[] AcceptedFileTypes = new string[] 
{	
  "jpg","jpeg",	"jpe",	"gif",	"bmp",	"png",
  "JPG","JPEG",	"JPE",	"GIF",	"BMP",	"PNG"
};

string FileErrorMessage = "";
string ValidationString = ".*(";
int i = 0;
while (i < AcceptedFileTypes.Length)
{
    ValidationString += "[\\." + AcceptedFileTypes[i] + "]";
    if (i < (AcceptedFileTypes.Length - 1))
    {
        ValidationString += "|";
    }
    FileErrorMessage += AcceptedFileTypes[i];
    if (i < (AcceptedFileTypes.Length - 1))
    {
       FileErrorMessage += ", ";
    }
    i += 1;
}
FileValidator.ValidationExpression = ValidationString + ")$";
FileValidator.ErrorMessage = FileErrorMessage;
 
Share this answer
 
ASP.NET
<body>
    <form id="Form1" method="post" runat="server" style="width: 530px;">
        <fieldset>
            <legend>Upload Multiple Photos</legend>
            <div id="divUpload"  runat="server">
                <input type="file" size="65" runat="server" id="FileUpload1"/>
                <input type="file" size="65" runat="server" id="FileUpload2"/>
                <input type="file" size="65" runat="server" id="FileUpload3"/>
                <input type="file" size="65" runat="server" id="FileUpload4"/>
                <input type="file" size="65" runat="server" id="FileUpload5"/>
            </div>
            <br />
            <asp:button id="btnUpload" runat="server" text="Upload" onclick="btnUpload_Click"/>
        </fieldset>
        <br />
        <div id="divShowImages"  runat="server" visible="false" style="overflow:auto;">
            <fieldset>
                <legend>Show Photos</legend>
                <div id="divImgUploaded"  runat="server">
                <asp:placeholder id="plhImgUploaded" runat="server" xmlns:asp="#unknown">
                </asp:placeholder>
                </div>
            </fieldset>
        </div>
    </form>
</body>

C#
protected void btnUpload_Click(object sender, EventArgs e)
   {
       divShowImages.Visible = true;
       HttpFileCollection fileCollection = Request.Files;
       int totalFilesUploaded = 0;
       //count the number of files.
       for (int i = 0; i < fileCollection.Count; i++)
       {
           if (fileCollection[i].ContentLength != 0)
           {
               totalFilesUploaded++;
           }
       }
       //declare a list<string>,
       //rename each file before saving and add the virtual path to the list<string> every time.
       List<string> lstPaths=new List<string>();
       string leasesubject = "PropertyImages", username = "username";
       for (int i = 0; i < totalFilesUploaded; i++)
       {
           HttpPostedFile file = fileCollection[i];
           string fileExt = Path.GetExtension(file.FileName).ToLower();
           string fileName = Path.GetFileName(file.FileName);
           if (fileName != string.Empty && (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png"))
           {
               string newFileName = leasesubject + username + new Random().Next(10000000, 999999999).ToString() + fileExt;
               file.SaveAs(HttpContext.Current.Server.MapPath("~/UploadedImages/" + leasesubject + "/" + newFileName));
               lstPaths.Add("~/UploadedImages/" + leasesubject + "/" + newFileName);
               Image img=new Image();
               img.ID="img"+i.ToString();
               img.Height=100;
               img.Width=100;
               img.ImageUrl = lstPaths[i];
               plhImgUploaded.Controls.Add(img);
           }
           else
           {
               Label lbl = new Label();
               lbl.Text ="you have entered invalid file type"+"<br />"+"Please Upload Images of Type .jpg, .gif, .bmp, .jpeg, .png";
               lbl.ID = "lblMsg";
               plhImgUploaded.Controls.Add(lbl);
           }

       }
 }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900