1

I need To find out the file type from a url image located on my server without checking the extension, but I'm not sure how to do that without putting the image into an "input" like this:

 <input type="file" id="upload_file" accept="image/*|audio/*|video/*"/>
 <input type="submit"  onclick="sumbit()"/>

<script type="text/javascript">
    function sumbit(){
        var file_Element = document.getElementById("upload_file")
        alert(file_Element.files[0].type);
        //alert: image/png
    } 
<script>

I understand that ".type" only work with a file object, so how do I turn the url image into an object like this image of google's logo: https://www.google.ca/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png. Do I need to use a ajax/flilereader? if so, how?

toastext
  • 195
  • 11
  • Possible duplicate of http://stackoverflow.com/q/18299806 – Drakes Jul 31 '16 at 00:19
  • Are you willing to do the file type detection server-side? – dionyziz Jul 31 '16 at 00:20
  • _"I need To find out the file type from a url image located on my server"_ How is image uploaded to your server? What do you mean by _"putting the image into an "input""_? How would you request image from your server without first being aware of file name and extension? – guest271314 Jul 31 '16 at 00:20
  • 1
    There is no guarantee that either the MIME type or the extension accurately identify the actual contents of the file. You can check the first few bytes of the file for a magic number, but this isn't 100% accurate. You probably want to be a lot more specific about what "type" you mean – Tibrogargan Jul 31 '16 at 00:23
  • Putting the image into an "input"? " html input tags" Had to re-edit the question, it wasn't showing it. – toastext Jul 31 '16 at 00:23
  • I understand MIME type or the extension accurately identify the actual contents. I rather not do it server-side, and let javascript handle it. – toastext Jul 31 '16 at 00:28

3 Answers3

2

The accept attribute value is not valid. There should be comma , instead of pipe | character separating MIME types.

You can use change event to check File object .type

<input type="file" id="upload_file" accept="image/*,audio/*,video/*"/>
 <input type="submit"  onclick="submit()"/>

<script type="text/javascript">
    var elem = document.getElementById("upload_file");
    elem.onchange = function(e) {
      console.log(e.target.files[0].type)
    }
    function submit() {
      if (elem.files.length) {
        console.log(elem.files[0].type)
      } else {
        alert("no files selected")
      }
    }
</script>
guest271314
  • 1
  • 10
  • 82
  • 156
  • Thanks, but I'm trying to avoid using input to solve my problem, because I'm grabing the data from the server, not from the client side.(: – toastext Jul 31 '16 at 00:36
  • @Danny Then why is `` at original Question? – guest271314 Jul 31 '16 at 00:36
  • becasue I didn' know what MIME object was until now. T^T And that was the only way I knew how to get the file type. – toastext Jul 31 '16 at 00:42
  • @Danny _"And that was the only way I knew how to get the file type."_ What do you mean by "get the file type"? – guest271314 Jul 31 '16 at 00:48
  • @ guest271314 data type of the file, example, "data:image/png;" – toastext Jul 31 '16 at 00:51
  • @Danny _"Example: data:image/png;"_ Not following. Can you create a plnkr http://plnkr.co, or jsfiddle http://jsfiddle.net to demonstrate? If you already had `MIME` type of file, what is purpose of present Question? – guest271314 Jul 31 '16 at 00:53
  • I didn't downvote you guest271314. I'll add one to make up for it. ;) – toastext Jul 31 '16 at 00:55
2

Use XHR to download the file, and then use the Blob api to determine the mime type:

var xhr = new XMLHttpRequest();
xhr.open('GET', '/path/to/image.png', true);
xhr.responseType = 'blob';

xhr.onload = function(e) {
    //Here's the type
    console.log(xhr.response.type);
};

xhr.send();
Goodbye StackExchange
  • 21,680
  • 7
  • 47
  • 83
  • This is what I'm looking for, I'm going to test it out. Thank you. – toastext Jul 31 '16 at 00:31
  • _"Use XHR to download the file"_ How can you download a file from user filesystem? Note, `File` object returned by `files` object of `` inherits from `Blob` – guest271314 Jul 31 '16 at 00:32
  • You wouldn't be downloading it from the users filesystem. The OP said "from a url image located on my server" – Goodbye StackExchange Jul 31 '16 at 00:35
  • @Danny _"This is what I'm looking for"_ What do you mean? Approach at Answer does not use ``? It is not possible to "download" a file from user filesystem – guest271314 Jul 31 '16 at 00:35
  • @FrankerZ What is purpose of creating a new `Blob` from `xhr.response`, which is a `Blob`? `var blob = new Blob([xhr.response]);` is not necessary; `xhr.response` is already a `Blob` – guest271314 Jul 31 '16 at 00:37
  • @guest271314: I'm grabing the data from the server, not from the client side. but the only way I knew how to do it was to import the file into the "HTML input tag" to grad the MIME type. – toastext Jul 31 '16 at 00:41
  • @Danny It is not possible to "import" a file into an `` element; `FileList` object is read-only. If you want to retrieve file from server you can use solution at this Answer; though note, creating a second `Blob` with `var blob = new Blob([xhr.response]);` is not necessary, you should be able to use `xhr.response.type`. – guest271314 Jul 31 '16 at 00:42
  • @ guest271314: yea you can: document.createElement("upload_file").setAttribute("value", "url"); – toastext Jul 31 '16 at 00:44
  • @Danny _"yea you can: document.createElement("upload_file").setAttribute("value", "url"); "_? No, that is not possible. `FileList` object will not contain `File` object from `"url"`, but rather, "fakepath" to file – guest271314 Jul 31 '16 at 00:46
  • @guest271314: I know. I'm creating a chat system, and I'm using AJAX to grab the data for the chat, thats why I can add in the value of the URL into the input. – toastext Jul 31 '16 at 00:48
  • @Danny _"I know. I'm creating a chat system, and I'm using AJAX to grab the data for the chat, thats why I can add in the value of the URL into the input."_ What do you mean? `"url"` at `document.createElement("upload_file").setAttribute("value", "url");` already contains the file extension? Returning the fakepath to the image? – guest271314 Jul 31 '16 at 00:51
  • @guest271314 the URL is store on my server and I'm grabing it and puting it into the input tag to grab the file type. It work, but it was inefficient and not the right way to do it. – toastext Jul 31 '16 at 01:00
  • @Danny _"the URL is store on my server and I'm grabing it"_ What is example of `URL`? How do retrieve URL? – guest271314 Jul 31 '16 at 01:18
  • I'm retrieving the URL with php. – toastext Jul 31 '16 at 01:55
2

Assuming your Content-Type HTTP headers are accurate, you can avoid downloading the whole file just to check the type by creating a HEAD request. Assuming you don't also need the whole file for something else, this could be a much-quicker operation, especially for large files.

Working Example:

var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'https://crossorigin.me/http://placehold.it/350x150', true);

xhr.onload = function() {
    var contentType = xhr.getResponseHeader('Content-Type');
    console.log(contentType);
};

xhr.send();

Alternately, you can achieve a similar result with a regular GET request by calling abort on the AJAX request object before it loads the whole body (in any remotely recent browser anyway).

Alternate Working Example:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://crossorigin.me/http://placehold.it/350x150', true);

xhr.onreadystatechange = function() {
    // Wait for header to become available.
    var contentType = xhr.getResponseHeader('Content-Type');
    if (contentType) {
        // Stop downloading, the headers are all we need.
        xhr.abort();
        console.log(contentType);
    }
};

xhr.send();
Alexander O'Mara
  • 52,993
  • 16
  • 139
  • 151
  • Thanks, I'll give this a try. (: – toastext Jul 31 '16 at 01:18
  • @AlexanderO'Mara Could also possibly use `HEAD` request. Though note _"the URL is store on my server and I'm grabing it and puting it into the input tag to grab the file type"_ at http://stackoverflow.com/questions/38679681/getting-a-file-type-from-url/38679738?noredirect=1#comment64739320_38679738. If OP already has URL, OP may already have file extension – guest271314 Jul 31 '16 at 01:20
  • @guest271314 Of course! I forgot about the `HEAD` request. Good call! – Alexander O'Mara Jul 31 '16 at 01:23
  • @guest271314 and AlexanderO'Mara! Thanks it works. (: – toastext Jul 31 '16 at 01:24