0

My english is not that good sorry...

hi i am working on a project where i have to upload diff types of files across my web app(MVC 5). i want to only allow the user to upload the file which is predifined. like if it is image only image can get uploaded and so on.

i have done this using custom validator and it works fine but i and also checked it while uplaoding

$("#FilUploader").change(function() {
    var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp'];
    if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) {
        alert("Only Images are allowed : " + fileExtension.join(', '));
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input type="file" accept="image/x-png,image/gif,image/jpeg,image/jpg" name="ImageUpload"id="FilUploader" />

like this but after the above javascript it will still upload the file in my model property which i have to check in my custom validator.

i also accept parameter in html but this is useless as it can be changed

i want something that wont allow the user to upload any other file and remove it in window itself

i don't want to use $('#file').val('')

Please help thanks in advance

Shiv Kumar Baghel
  • 2,464
  • 6
  • 18
  • 34
Mohit
  • 15
  • 9
  • Maybe use accept attribute? https://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file – A. Meshu Sep 23 '18 at 15:54
  • 1
    "but this is useless as it can be changed" - there is nothing you can do on the client side to stop a malicious actor uploading a file you didn't ask for. Validation has to be on the server side to stop that. – Eric Sep 23 '18 at 15:57
  • 1
    @A.Meshu: The `Accept` HTTP header you previously linked to is for clients telling the server what they accept, not the other way around. – Eric Sep 23 '18 at 15:57
  • 2
    Possible duplicate of [Limit file format when using ?](https://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file) – Eric Sep 23 '18 at 15:59
  • @A.Meshu i am using accept attribute but it only shows custom file in upload window which can easily be changed – Mohit Sep 23 '18 at 16:10
  • @Eric you are right i want to this on client side i have already done it on serverside I am Looking for something on client side something that wont let user from uploading any other type of file – Mohit Sep 23 '18 at 16:11

0 Answers0