Fileupload: allow only .jpg .gif and .pnghttp://forums.asp.net/t/1051895.aspx/1?Fileupload+allow+only+jpg+gif+and+pngWed, 21 Jul 2010 06:53:23 -040010518951488103http://forums.asp.net/p/1051895/1488103.aspx/1?Fileupload+allow+only+jpg+gif+and+pngFileupload: allow only .jpg .gif and .png <p>Hi all</p> <p>I programmed a FileUpload which is already working fine. Now I'd like to add a feature: At the form where I browse for the file I want to upload, I'd like to validate the file type.</p> <p>I'd like to allow only image files in the formats .jpg, .gif and .png and show an error message if the file is not valid (= if the format is not allowed)<br> Does anybody know how i can program this feature?</p> 2006-12-06T13:10:53-05:001488144http://forums.asp.net/p/1051895/1488144.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>i can always rename my a.exe to a.jpg, can i not?</p> <p>but i guess you could try/catch opening the uploaded stream as a .NET image, and reject the file if exception is caught.</p> <p>&nbsp;</p> 2006-12-06T13:39:26-05:001488423http://forums.asp.net/p/1051895/1488423.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>how about using a regular expression validator?</p> <p>&nbsp;</p> 2006-12-06T16:31:16-05:001489019http://forums.asp.net/p/1051895/1489019.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>hi,</p> <p>look at this</p> <h3 class="dtH1">Client-Side Validation of File Types Permissible to Upload</h3> <p>There are several methods you can use to control the types of files that are uploaded to the server. Unfortunately, there is no bullet-proof method to protect you from someone uploading files that would be considered malicious. You can take a few steps, however, to make this process of allowing end users to upload files a little more manageable.</p> <p>One nice method you can employ is to use the ASP.NET validation controls that are provided for free with ASP.NET. These controls enable you to do a regular-expression check upon the file that is being uploaded to see if the extension of the file is one you permit to be uploaded.</p> <p>This is ideal for browsers that allow client-side use of the validation controls because it forces the checking to be done on the client; the file is not uploaded to the server if the signature isn't one you allow. Listing 3 shows you an example of using validation controls to accomplish this task.</p> <blockquote class="dtBlock"><b>Note</b> The use of validation controls is not explained here. Take a look at <a href="http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/ASPNet-ValidateASPNetServerControls.asp"> <font color="#0061de">Validating ASP.NET Server Controls</font></a> for a complete explanation of validation controls and how to use them in your ASP.NET pages.</blockquote> <p class="label"><b>Listing 3. Using validation controls to restrict the types of files uploaded to the server</b></p> <pre class="code">&lt;asp:FileUpload ID=&quot;FileUpload1&quot; runat=&quot;server&quot; /&gt;&lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; OnClick=&quot;Button1_Click&quot; Text=&quot;Upload File&quot; /&gt;&amp;nbsp;&lt;br /&gt; &lt;br /&gt; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot;&gt;&lt;/asp:Label&gt; &lt;asp:RegularExpressionValidator id=&quot;RegularExpressionValidator1&quot; runat=&quot;server&quot; ErrorMessage=&quot;Only mp3, m3u or mpeg files are allowed!&quot; ValidationExpression=&quot;^(([a-zA-Z]:)|(\\{2}\w&#43;)\&#36;?)(\\(\w[\w].*)) &#43;(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)&#36;&quot; ControlToValidate=&quot;FileUpload1&quot;&gt;&lt;/asp:RegularExpressionValidator&gt; &lt;br /&gt; &lt;asp:RequiredFieldValidator id=&quot;RequiredFieldValidator1&quot; runat=&quot;server&quot; ErrorMessage=&quot;This is a required field!&quot; ControlToValidate=&quot;FileUpload1&quot;&gt;&lt;/asp:RequiredFieldValidator&gt; </pre> <p>This simple ASP.NET page uses validation controls so that the end user can only upload .mp3, .mpeg, or .m3u files to the server. If the file type is not one these three choices, a <b>Validation</b> control throws an exception onto the screen. This is shown in Figure 4.</p> <p class="fig"><img alt="" src="http://msdn.microsoft.com/library/en-us/dnaspp/html/uploadasp204.gif" border="0"></p> <p class="label"><b>Figure 4. Validating the file type using validation controls</b></p> <p>Using <b>Validation</b> controls is not a foolproof way of controlling the files that are uploaded to the server. It wouldn't be too hard for someone to change the file extension of a file so it would be accepted and uploaded to the server, thereby bypassing this simple security model.</p> <h3 class="dtH1">Adding Server-Side File Type Validation</h3> <p>You just saw an easy way to add some ASP.NET validation server controls to your ASP.NET page to perform a client side validation of the file extension (in just a textual manner). Now let's take a look at how to perform a similar operation on the server-side. This is presented in Listing 4.</p> <p class="label"><b>Listing 4. Checking the file type on the server</b></p> <p class="label"><b>Visual Basic</b></p> <pre class="code"> Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) If FileUpload1.HasFile Then Dim fileExt As String fileExt = System.IO.Path.GetExtension(FileUpload1.FileName) If (fileExt = &quot;.mp3&quot;) Then Try FileUpload1.SaveAs(&quot;C:\Uploads\&quot; &amp; _ FileUpload1.FileName) Label1.Text = &quot;File name: &quot; &amp; _ FileUpload1.PostedFile.FileName &amp; &quot;&lt;br&gt;&quot; &amp; _ &quot;File Size: &quot; &amp; _ FileUpload1.PostedFile.ContentLength &amp; &quot; kb&lt;br&gt;&quot; &amp; _ &quot;Content type: &quot; &amp; _ FileUpload1.PostedFile.ContentType Catch ex As Exception Label1.Text = &quot;ERROR: &quot; &amp; ex.Message.ToString() End Try Else Label1.Text = &quot;Only .mp3 files allowed!&quot; End If Else Label1.Text = &quot;You have not specified a file.&quot; End If End Sub </pre> <p class="label"><b>C#</b></p> <pre class="code"> protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName); if (fileExt == &quot;.mp3&quot;) { try { FileUpload1.SaveAs(&quot;C:\\Uploads\\&quot; &#43; FileUpload1.FileName); Label1.Text = &quot;File name: &quot; &#43; FileUpload1.PostedFile.FileName &#43; &quot;&lt;br&gt;&quot; &#43; FileUpload1.PostedFile.ContentLength &#43; &quot; kb&lt;br&gt;&quot; &#43; &quot;Content type: &quot; &#43; FileUpload1.PostedFile.ContentType; } catch (Exception ex) { Label1.Text = &quot;ERROR: &quot; &#43; ex.Message.ToString(); } } else { Label1.Text = &quot;Only .mp3 files allowed!&quot;; } } else { Label1.Text = &quot;You have not specified a file.&quot;; } } </pre> <p>Now, by using the <b>GetExtension</b> method from the <b>System.IO.Path</b> namespace, you can perform basically the same operation. It is important to note that this doesn't get around an end user's ability to simply change the file extension to something that works and upload that altered file to the hosting server.</p> 2006-12-07T02:10:35-05:001489031http://forums.asp.net/p/1051895/1489031.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>one way you can check is by peeking at the filestream in the posted file. </p> <p>you can open a reader and take the first 10 or so bytes.&nbsp; And from the header info you can usually tell what type of file it is. <br> Its not bulletproof however, because the location of the filetype info is not always in the same place or format. </p> <p>heres an if i used to check for valid image types a while ago. i just passed it&nbsp;the first chunk in my upload</p> <p>&nbsp;<pre class="prettyprint">//checks if it is an image private bool IsImage(byte[] data) { //read 64 bytes of the stream only to determine the type string myStr = System.Text.Encoding.ASCII.GetString(data).Substring(0,16); //check if its definately an image. if(myStr.Substring(8,2).ToString().ToLower()!=&quot;if&quot;) { //its not a jpeg if(myStr.Substring(0,3).ToString().ToLower() != &quot;gif&quot;) { //its not a gif if(myStr.Substring(0,2).ToString().ToLower() != &quot;bm&quot;) { //its not a .bmp if(myStr.Substring(0,2).ToString().ToLower() != &quot;ii&quot;) { //its not a tiff //ProcessErrors(&quot;notImage&quot;); this.myFile.PostedFile.InputStream.Close(); myStr = null; return false; } } } } myStr = null; return true; }</pre>&nbsp;hth<br> mcm</p> 2006-12-07T02:24:25-05:001489121http://forums.asp.net/p/1051895/1489121.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p><strong>I like mcmcomasp's solution slightly better. </strong></p> <p>&nbsp;<br> <strong>You realize that you are doing all that work and I can just rename my file? Spend your time on other features of your project, I'd suggest.</strong></p> <p>&nbsp;</p> 2006-12-07T03:52:21-05:002164399http://forums.asp.net/p/1051895/2164399.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>WoW, Egypt Winnnnnnnn,[:D][:D]<br> and I just found the solution for that problem<br> try this if you want <br> <br> ___________________________</p> <p>&nbsp;</p> <pre class="prettyprint">&lt;script language=&quot;javascript&quot;&gt; function ValidateFile(source, args) { try { var fileAndPath= document.getElementById(source.controltovalidate).value; var lastPathDelimiter=fileAndPath.lastIndexOf(&quot;\\&quot;); var fileNameOnly=fileAndPath.substring(lastPathDelimiter&#43;1); var file_extDelimiter=fileNameOnly.lastIndexOf(&quot;.&quot;); var file_ext=fileNameOnly.substring(file_extDelimiter&#43;1).toLowerCase(); if(file_ext!=&quot;jpg&quot;) { args.IsValid = false; if(file_ext!=&quot;gif&quot;) args.IsValid = false; if(file_ext!=&quot;png&quot;) { args.IsValid = false; return; } } }catch(err) { txt=&quot;There was an error on this page.\n\n&quot;; txt&#43;=&quot;Error description: &quot; &#43; err.description &#43; &quot;\n\n&quot;; txt&#43;=&quot;Click OK to continue.\n\n&quot;; txt&#43;=document.getElementById(source.controltovalidate).value; alert(txt); } args.IsValid = true; } &lt;/script&gt; &lt;asp:FileUpload ID=&quot;FileUpload1&quot; runat=&quot;server&quot; /&gt; &lt;asp:CustomValidator ID=&quot;CustomValidator1&quot; ClientValidationFunction=&quot;ValidateFile&quot; runat=&quot;server&quot; ControlToValidate=&quot;FileUpload1&quot; Display=&quot;dynamic&quot; ErrorMessage=&quot;images only &quot;&gt; &lt;/asp:CustomValidator&gt;</pre>&nbsp;<br> &nbsp;&nbsp; <br> &nbsp;____________________________<br> <p><br> please post comments if there any comment,I'm not sure about it yet, <br> </p> <p>Thank you <br> </p> 2008-02-10T19:53:33-05:002168353http://forums.asp.net/p/1051895/2168353.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>while this solution is still workable the problem still exists where a user can easily change the extension of the filename to bypass this check.&nbsp; the only &quot;sure fire&quot; way to know if its an image is to peek at the file stream as i demonstrated above. </p> <p>hth,</p> <p>mcm </p> 2008-02-12T14:19:47-05:002171502http://forums.asp.net/p/1051895/2171502.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>Well, I know that I can't check if it's an image of not at client side , but not all users think to change file extension ,&nbsp; but we can use that solution as indicator to the user that we accept images only, and at server side we can use your &quot;sure fire&quot; way to detect images inly, anyway Great Topic , thank you very much.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>mcmcomasp</h4> <p></p> <p>while this solution is still workable the problem still exists where a user can easily change the extension of the filename to bypass this check.&nbsp; the only &quot;sure fire&quot; way to know if its an image is to peek at the file stream as i demonstrated above. </p> <p>hth,</p> <p>mcm </p> </blockquote> &nbsp; 2008-02-13T19:37:06-05:002476930http://forums.asp.net/p/1051895/2476930.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>&nbsp;That's pretty funny the FileUpload control's contentType can lie to you. I guess it just loads the file into a byte[] and let's you take it from there.<br> <br> I think you can test the real file contents information by loading the file into a System.Drawing.Image and testing it's RawFormat property. There, you will find a Guid that is unique for each image type. If you can't load the file into an Image, I guess it's not a jpg, png or gif as you requested.<br> <br> <br> System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);<br> <br> if (img.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.Jpeg.Guid) {do something}<br> <br> Or, if you want to return the type of the image, try this:<br> <br> &nbsp;&nbsp; &nbsp;public static string MimeType(System.Drawing.Image imgPhoto)<br> &nbsp;&nbsp; &nbsp;{<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders())<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (codec.FormatID == imgPhoto.RawFormat.Guid)<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return codec.MimeType;<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return &quot;image/unknown&quot;;<br> &nbsp;&nbsp; &nbsp;}</p> 2008-07-09T11:02:28-04:002477885http://forums.asp.net/p/1051895/2477885.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>this is another good idea as well.&nbsp; One problem with peeking into the stream is sometimes you can get errors with uploading the file,&nbsp; i have found that i actually have to dump the stream and re-upload the whole thing from the beginning after peeking at the first few bytes, if not sometimes the images does not re-assemble itself.&nbsp; This looks like a good solution if it works.</p> <p>mcm </p> 2008-07-09T16:31:02-04:003389938http://forums.asp.net/p/1051895/3389938.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>&nbsp;&nbsp; if (FileUpload1.HasFile)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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p; }</p> <p><br> </p> <p>i have written this code. maybe it will help you. try this one<br> </p> 2009-09-06T09:48:35-04:003457044http://forums.asp.net/p/1051895/3457044.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>Let me introduce || (or)&nbsp;and &amp;&amp; (and)&nbsp;to you&nbsp;<strong>tsolbayar </strong>:) &nbsp;</p> <p>write your if check like this using ||</p> <p>if (file_ext == &quot;.BMP&quot; || ile_ext == &quot;.BMP&quot; || file_ext == &quot;.PNG&quot;)</p> <p>&nbsp;</p> <p>It shouldn't matter if a .exe file is being renamed to .jpg or .gif or whatever because it won't execute the file unless it's .exe. The image browser will simply try to open the file and get a error as it's not reconizeable. But as mentioned I think you'll get the problem sorted if you load the image into a System.Drawing.Image, and use the FromStream() method, then you could also validate the image size and dimension&nbsp;to make sure it's not huge.</p> <p>A bit of late reply but if some people are still searching this forums as I were then it could be handy.</p> 2009-10-14T06:40:37-04:003631200http://forums.asp.net/p/1051895/3631200.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>I believe there is much easier way how to find out whether posted binary data are image or not:</p> <p><br> </p> <p><br> </p> <pre class="prettyprint">Image uploadedImage = null; if (ImageUpload.HasFile &amp;&amp; ImageUpload.FileName != string.Empty &amp;&amp; ImageUpload.FileContent.Length &gt; 0) { try { uploadedImage = Image.FromStream(ImageUpload.PostedFile.InputStream); } catch (Exception ex) { lblUploadStatus.Text = &quot;Selected file is not an image.&lt;br /&gt;&quot; &#43; ex.Message; } if (uploadedImage != null) { string savePath = string.Format(&quot;{0}/{1}&quot;, Server.MapPath(&quot;~/images/orig/upload_temp&quot;), ImageUpload.FileName); uploadedImage.Save(savePath, ImageFormat.Jpeg); } }</pre> <p><br> Hope this will help.<br> </p> 2010-01-21T08:52:14-05:003632462http://forums.asp.net/p/1051895/3632462.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p></p> <blockquote><span class="icon-blockquote"></span> <h4>jessjing</h4> You just saw an easy way to add some ASP.NET validation server controls to your ASP.NET page to perform a client side validation of the file extension (in just a textual manner). Now let's take a look at how to perform a similar operation on the server-side.</blockquote> &nbsp; <p></p> <p>It's&nbsp;a&nbsp;common mistake is to think that Validation Controls only work on client side, however, they&nbsp;ALSO work on&nbsp;server side, so why would you need to implement this code?</p> <p>&nbsp;</p> <p>&nbsp;</p> 2010-01-21T20:21:20-05:003632471http://forums.asp.net/p/1051895/3632471.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sensei_cz1</h4> I believe there is much easier way how to find out whether posted binary data are image or not</blockquote> &nbsp; <p></p> <p>Even if you're sure that you're dealing with an image, you still not sure!</p> <p><a href="/t/1514476.aspx">http://forums.asp.net/t/1514476.aspx</a></p> 2010-01-21T20:24:22-05:003632481http://forums.asp.net/p/1051895/3632481.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p></p> <blockquote><span class="icon-blockquote"></span> <h4>moud_gersy</h4> and I just found the solution for that problem<br> </blockquote> &nbsp; <p></p> <p>Client side scripts can never be considered a solution, because you cannot trust them. At the most, they can help, preventing unneccesary postbacks to the server and provide a richer UI, but that's it! All validation should be done at serverside also!</p> 2010-01-21T20:30:54-05:003632505http://forums.asp.net/p/1051895/3632505.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>&nbsp;glad to see this thread still sparks debate all this time later. </p> <p>hans v is right.&nbsp; client side script can only be used to filter blatent extension differences - a server side solution is the only way to determine if a file is really of a given type.&nbsp;&nbsp; reading the file stream is still my favourite but i do like attempting to create an image from the stream and seeing if that returns a valid image or not.&nbsp; It would be interesting to see if any malicious file types can either: </p> <p>a) mimic an imagetype for the first (x) bytes of the stream....</p> <p>b) trick the Image.FromStream method to create an image (dont know if that was the exact method name but you get what i saying) </p> <p>The image from stream method though probably requires the whole file to already be transmitted to the server, where reading the stream you can just grab a subset and dump it if you want too. </p> <p>mcm</p> <p>&nbsp;</p> 2010-01-21T20:43:20-05:003691205http://forums.asp.net/p/1051895/3691205.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p>go to&nbsp; http://southdesk.com/content/aspnet-file-uploader-validation-file-type-and-file-size to see solution<br> </p> 2010-02-20T21:04:54-05:003691206http://forums.asp.net/p/1051895/3691206.aspx/1?Re+Fileupload+allow+only+jpg+gif+and+pngRe: Fileupload: allow only .jpg .gif and .png <p><br> </p> 2010-02-20T21:05:48-05:00