Restrict File Upload to Some File Extensions

Limit file format when using input type=file?

Strictly speaking, the answer is no. A developer cannot prevent a user from uploading files of any type or extension using front-end validation (HTML/JavaScript).

But still, the accept attribute of <input type = "file"> can help to provide a filter in the file select dialog box provided by the user's browser/OS. For example,

<!-- (IE 10+, Edge (EdgeHTML), Edge (Chromium), Chrome, Firefox 42+) --> 
<input type="file" accept=".xls,.xlsx" />

input type=file limit selectable files by extensions

Honestly, the best way to limit files is on the server side. People can spoof file type on the client so taking in the full file name at server transfer time, parsing out the file type, and then returning a message is usually the best bet.

restrict file upload selection to specific types

There is an html attribute for this specific purpose called accept but it has little support across browsers. Because of this server side validation is recommended instead.

<input type="file" name="pic" id="pic" accept="image/gif, image/jpeg" />

If you don't have access to the backend have a look at a flash based solution like SWFUpload.

See more on this here: File input 'accept' attribute - is it useful?

Restrict file upload to some file extensions

The answer to this question is probably more related to html uploading than rails.

When you want to upload a file, you typically do an input with type="file".

This can be done in Rails by using the file_field_tag helper. It will generate an input with type="file" which can also have an accept attribute, but you can't really use that because it's not really going to have any visible effect. This attribute accepts MIME types, not extensions, and most browsers don't even use it.

The best thing you can do is probably have a javascript check the file extension before upload (after you select the file from the dialog box). Read more about it in this question.

The point is, you can't force the OS to show you only the file extensions that you want. You can either validate the extension by using JS for example, before upload, or check the contents of the file after upload, server side

How to restrict file extension in operating System dialog box from HTML form

In HTML5 you can use "accept" attribute in the input[type=file] tag. For example:

<form action="" method="POST" " enctype="multipart/form-data">    
<input type="file" name="files" accept="image/jpeg,image/png,image/gif/>
<input type="submit" name="upload" value="Upload" placeholder="Only .jpg/.jpeg files support."/>
</form>

Restrict file type to be upload

Use Path.GetExtension Then you can have something like

string fileExtension = Path.GetExtension(fileName);
fileExtension = fileExtension.ToLower();
string[] acceptedFileTypes = { ".docx", ".pdf" };
bool acceptFile = false;

for (int i = 0; i <= 1; i++)
{
if (fileExtension == acceptedFileTypes[i])
{
acceptFile = true;
}
}

if (!acceptFile)
{
Label1.Text = "You error message here";
return;
}

How to restrict the files based on extension using Angular?

Please try this.It works for me.

var regexp;
var extension = fileName.substr(fileName.lastIndexOf('.'));
if ((extension.toLowerCase() == ".exe") ||
(extension.toLowerCase() == ".twbx"))
{
alert("Could not allow to upload .exe and .twbx files");
}

How do I restrict file upload to certain extensions on Rails

You shouldn't really because it's a security risk. Rather use a gem like Paperclip or Carrierwave. There are Railscasts for both to get you started on using them.

How to have jQuery restrict file types on upload?

You can get the value of a file field just the same as any other field. You can't alter it, however.

So to superficially check if a file has the right extension, you could do something like this:

var ext = $('#my_file_field').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
alert('invalid extension!');
}


Related Topics



Leave a reply



Submit