35

Is it possible to allow the fileupload control to show only images?

When we click the Browse button it should show only images.

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
Geeth
  • 5,282
  • 21
  • 82
  • 133

8 Answers8

68

In 2015, web browsers support the input accept attribute, so you can do this:

<asp:FileUpload ID="fileUploader" runat="server" accept=".png,.jpg,.jpeg,.gif" />

Keep in mind Visual Studio may show you a message about this as an invalid attribute of the FileUpload ASP tool, however.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Tonko Boekhoud
  • 819
  • 6
  • 9
  • 7
    This will still allow a user to upload a file that doesn't use one of those extensions – jtate Nov 29 '16 at 16:57
  • 3
    @jtate The question is limited to *show* only images. You should always validate the upload (and any user input) on the server side, to catch somebody trying to upload something unwanted. User could simply rename the extension of an unwanted file to something in the accept list. So you should approach the accept list in the file upload as a simple help to the user and at least validate on the back end. – Tonko Boekhoud Dec 05 '16 at 07:37
  • That's a very good solution. Just updating here that it does not have any effect in Microsoft Edge browser., or else it works on IE10/11, Firefox, Chrome and Safari. Thanks! – kaushalparik27 Aug 16 '19 at 06:20
22

I found no direct solution for this problem.

This is my workaround using the RegularExpressionValidator:

<asp:FileUpload ID="fuImportImage" runat="server" />
<asp:RegularExpressionValidator ID="regexValidator" runat="server"
     ControlToValidate="fuImportImage"
     ErrorMessage="Only JPEG images are allowed" 
     ValidationExpression="(.*\.([Jj][Pp][Gg])|.*\.([Jj][Pp][Ee][Gg])$)">
</asp:RegularExpressionValidator>
Christoph Brückmann
  • 1,373
  • 1
  • 23
  • 41
14

You cannot strictly restrict the file type, but if the browser supports it you can cause it to initially show just a certain type of file:

<form method="post" action="blahblah.blah">
  <input type="file" name="image" id="image" accept="image/png, image/jpeg" />
</form>
iconoclast
  • 21,213
  • 15
  • 102
  • 138
8

No, in web you can't from client side, evidently from server side you can do amazing things. For this kind of thing, programmers normally use Activex, flash or the like.

netadictos
  • 7,602
  • 2
  • 42
  • 69
  • 5
    New web browsers support the input accept attribute, so you can do this: `accept=".png,.jpg,.jpeg,.gif" ` – Learning Sep 08 '19 at 09:20
  • that doesn't work. In Chrome it appears to restrict the file picker to image types but you can change the dropdown to (all files *.*) and upload a non image file. – Norbert Norbertson Sep 14 '20 at 07:47
3
//VALIDATE FILE EXTENTION
var _validFileFlag;
function fValidFileExt(vfilePath){
  var vFileName=vfilePath.split('\\').pop();
  var vFileExt=vfileName.split('.').pop();
  if(vFileExt.toUpperCase()=="JPEG" || vFileExt.toUpperCase()=="JPG"){
     _validFileFlag = true;
  } 
  _validFileFlag = false;
} 

<asp:FileUpload ID="FileUpload1" onchange="fValidFileExt(this.value);" runat="server"  />

Check '_validFileFlag' while saving data/upload..

Somnath R
  • 31
  • 2
2

Assuming you mean uploading images only.

You can check the ContentType property of the file (I.e. image/gif).

Take a look here for an example: https://web.archive.org/web/20100306030822/http://www.15seconds.com/issue/061116.htm

David Neale
  • 16,498
  • 6
  • 59
  • 85
2

Use accept attribute to show only images in file browser like below -

<asp:FileUpload ID="FileUploadFileType" runat="server" CssClass="form-control" accept=".png,.jpg,.jpeg,.gif" />

with asp.nets RegularExpressionValidator to validate selected file type with validation message.

<asp:RegularExpressionValidator ID="RegExValFileUploadFileType" runat="server"
                        ControlToValidate="FileUploadFileType"
                        ErrorMessage="Only .jpg,.png,.jpeg,.gif Files are allowed" Font-Bold="True"
                        Font-Size="Medium"
                        ValidationExpression="(.*?)\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)$"></asp:RegularExpressionValidator>
Ravindra Vairagi
  • 1,055
  • 15
  • 22
1

With plain <input type="file">, I am afraid it's not possible on the client-side.

However, some 3rd party uploader such as SWFUpload provides this functionality.

Gant
  • 29,661
  • 6
  • 46
  • 65