简体   繁体   中英

Check file extension before importing file

I have a requirement where I have to import a file from the local using javascript jquery. Depending on the type of file that user has selected the displaying logic is used. The flow is such: User selects on 'Import' and selects the file from local. As soon as he says okay I need to retrieve the file extension that has been selected and depending on that need to display the file.

Here is the plunker working code

You will be getting the mime type which is the safest way to check. I just made the alert box of mime type of the code.

<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="submit" value="Upload" />
</form>


<script>
$('input[type=file]').change(function(){
alert('To check whether i am getting into the script '); //Comment this
var file = this.files[0];
name = file.name;
size = file.size;
type = file.type;
alert(type);
//your validation
});
</script>

Credits : Goes To

I have been using the below script Check file extension var file_name="file.jepg"; var extension = /.(\\w+)$/.exec(file_name)[1]; if(extension=="condition") {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM