How can I check if the browser support HTML5 file upload (FormData object)?
Asked Answered
G

7

31

How can I check if the browser support HTML5 file upload (FormData object)?

var fd = new FormData();

Following the answer from this post, but the code does not return correct answer about the browser,

window.onload = function()
{
 if (!!window.FileReader)
 {
  alert('supported');
 }
 else
 {
  alert('not supported');
 }
}


Firefox - supported
Chrome - supported
Opera - supported
Safari - not supported
IE9 - not supported

But the correct browser support should be,

Firefox - supported
Chrome - supported
Opera - not supported
Safari - supported
IE9 - not supported

I have tested the html 5 file upload on Opera and it is not working for sure.

I am sure that safari supports html 5 file upload.

Gazpacho answered 3/9, 2011 at 23:19 Comment(0)
W
70

Try if( window.FormData === undefined ) or if( window.FormData !== undefined ).

Wiedmann answered 27/12, 2011 at 19:47 Comment(6)
+1, in IE8/IE9 ( window.FormData === undefined ) returns true and in IE10, it returns false.Unboned
Should probably use typeof, since undefined is not a keyword in some browsers. (e.g. I can say var undefined = window.FormData)Dryer
So, with typeof, "if( typeof( window.FormData ) == 'undefined' )", right?Wershba
@Wershba no need for the parens: if(typeof window.FormData === 'undefined')Sible
@Koen Just because they're optional doesn't mean they're forbidden. I prefer to use the parentheses.Wershba
I didn't say that they're forbidden. But do you write +, - and other "unary operators" with parentheses too? developer.mozilla.org/en-US/docs/Web/JavaScript/…Sible
P
11

From http://blog.new-bamboo.co.uk/2010/7/30/html5-powered-ajax-file-uploads

function supportAjaxUploadProgressEvents() {
    var xhr = new XMLHttpRequest();
    return !! (xhr && ('upload' in xhr) && ('onprogress' in xhr.upload));
};

As FormData, the ability to send() one, and the upload property (and its onprogress event) are all part of XMLHttpRequest level 2, you can test for .upload to see if you've got a level 2. I don't have a Mac handy, but the function (sadly, but correctly) returns false for Opera 11.50 (and true for Firefox 4).

Pycno answered 21/9, 2011 at 4:8 Comment(0)
D
7
  function supportFormData() {
     return !! window.FormData;
  }

Source: https://www.new-bamboo.co.uk/blog/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata/

Distressful answered 19/10, 2014 at 13:28 Comment(0)
C
3

This is the one-liner I use to check if the browser supports FormData and upload progress, in jQuery:

 var xhr2 = !! ( window.FormData && ("upload" in ($.ajaxSettings.xhr()) );
Coretta answered 20/8, 2012 at 15:38 Comment(0)
T
1

You could may be use the workaround provided by this library. https://github.com/francois2metz/html5-formdata

Tenacious answered 7/2, 2012 at 6:27 Comment(0)
A
1

On Safari 5.1.7 , Firefox <6, Opera < 12.14 form data is suported but it is buggy.

  • Safari will return file size 0

    Opera does not support append method of form data

    firefox <6 does not work correctly

Audry answered 3/3, 2013 at 12:58 Comment(0)
D
-3

You need to check if the browser supports the HTML5 file API. I do that by checking if the FileReader function is set, if it’s not set it means that the browser won’t support the file API.

// Check if window.fileReader exists to make sure the browser supports file uploads
if (typeof(window.FileReader) == 'undefined') 
    {
        alert'Browser does not support HTML5 file uploads!');
    }
Diastyle answered 14/12, 2011 at 18:2 Comment(1)
This code checks for the FileReader object, which is different than the FormData object.Wiedmann

© 2022 - 2024 — McMap. All rights reserved.