1

Could you tell me (for example, Google could not), if I can set the default file format for file upload.

Currently it allows to upload all files (please refer to the file image *.*), however I would like to limit to a specific file format.

All help is appreciated.

enter image description here

Environment:

NO HTML5

Backend: Struts

FrontEnd: jQuery-1.6.1

File upload plugin uses iframe to upload files.

2
  • 1
    This might be of interest to you: stackoverflow.com/a/4328974/551093 Commented May 30, 2012 at 2:10
  • Thanks, however a guy wants to restrict, rather than set a preselected (default) file format. Commented May 30, 2012 at 2:39

1 Answer 1

1

you can use this

<input type="file"  id="myfile" accept="image/gif, image/jpeg, image/png, image/jpeg" />

but using this. user can anytime change the filter. additionally you should use javascript or jquery to validate.

<script type ="text/javascript">
    var validFiles=["bmp","gif","png","jpg","jpeg"];//array of allowed extensions
        function OnUpload()
        {
          var obj = document.getElementById("myfile");
          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("This not a valid file upload file with an extention of one of the following:\n\n"+validFiles.join(", "));
            return false;
          }
          return true;
         }
    </script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks however it is validation, rather then selecting a default OS file type.
but I am afraid, this is your only option. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.