Umidjon Urunov
Umidjon Urunov

Reputation: 671

Get Original file type using jQuery

How can I check the file type (original type) with JQuery? I mean even if I change the file format manually from .pdf to .jpg, I should get .pdf because this file is PDF document not picture. I am using the following code for checking file type :

var fileExtension = ['jpeg', 'jpg', 'png'];
if ($.inArray(file_input.val().split('.').pop().toLowerCase(), fileExtension) == -1) {
  alert("Wrong format!");       
}
else {
  alert("Correct format!");
}

Upvotes: 3

Views: 5765

Answers (1)

stark
stark

Reputation: 2256

Borrowing the JS from @Drakes answer here.

I've posted the JQuery Implementation for the same below :

$('#show').on("click", function() {
  var files = $("#your-files")[0].files[0];
  var temp = "";
  temp += "<br>Filename: " + files.name;
  temp += "<br>Type: " + files.type;
  temp += "<br>Size: " + files.size + " bytes";
  $('#details').html(temp);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="your-files" multiple>
<button id="show">
  Show Details
</button>
<div id="details">
</div>

You can then use the file type that you get using the above jQuery in the control statements of the code posted in your question.

Upvotes: 2

Related Questions