Javascript 如何检查浏览器是否支持 HTML5 文件上传(FormData 对象)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7296426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:40:28  来源:igfitidea点击:

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

javascriptfile-uploadsafarihtml5-videoopera

提问by laukok

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

如何检查浏览器是否支持 HTML5 文件上传(FormData 对象)?

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 Operaand it is notworking for sure.

我已经在Opera上测试了 html 5 文件上传,但它确实无法正常工作。

I am sure that safarisupports html 5 file upload.

我确信safari支持 html 5 文件上传。

回答by webinista

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

尝试if( window.FormData === undefined )if( window.FormData !== undefined )

回答by Leon

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

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

作为 FormData,send() 的能力和上传属性(及其 onprogress 事件)都是 XMLHttpRequest 级别 2 的一部分,您可以测试 .upload 以查看您是否达到了级别 2。我没有手边有 Mac,但该函数(遗憾但正确)在 Opera 11.50 中返回 false(对于 Firefox 4 返回 true)。

回答by Wavy Davy

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

这是我用来检查浏览器是否支持 FormData 和上传进度的单行代码,在 jQuery 中:

 var xhr2 = !! ( window.FormData && ("upload" in ($.ajaxSettings.xhr()) );

回答by albanx

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

在 Safari 5.1.7 上,支持 Firefox <6、Opera < 12.14 表单数据,但有问题。

  • Safari will return file size 0

    Opera does not support append method of form data

    firefox <6 does not work correctly

  • Safari 将返回文件大小 0

    Opera 不支持表单数据的追加方法

    firefox <6 不能正常工作

回答by Sujay

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

您可以使用此库提供的解决方法。 https://github.com/francois2metz/html5-formdata

回答by programmer guy

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.

您需要检查浏览器是否支持 HTML5 文件 API。我通过检查 FileReader 函数是否已设置来做到这一点,如果未设置,则意味着浏览器将不支持文件 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!');
    }