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

三世轮回 提交于 2019-11-28 04:55:51

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

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).

  function supportFormData() {
     return !! window.FormData;
  }

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

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()) );

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

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

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!');
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!