簡體   English   中英

如何檢查瀏覽器是否支持HTML5文件上傳(FormData對象)?

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

如何檢查瀏覽器是否支持HTML5文件上傳(FormData對象)?

var fd = new FormData();

根據這篇文章的回答,但代碼沒有返回關於瀏覽器的正確答案,

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


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

但正確的瀏覽器支持應該是,

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

我已經測試了Opera上的html 5文件上傳,但它無法確定。

我相信safari支持html 5文件上傳。

嘗試if( window.FormData === undefined )if( window.FormData !== undefined )

來自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));
};

作為FormData,send()one和upload屬性(及其onprogress事件)的能力都是XMLHttpRequest級別2的一部分,你可以測試.upload以查看你是否有2級。我不知道有一個Mac方便,但功能(遺憾的是,但正確)返回Opera 11.50的假(對於Firefox 4也是如此)。

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

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

這是我在jQuery中用於檢查瀏覽器是否支持FormData和上傳進度的單行程序:

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

在Safari 5.1.7上,Firefox <6,Opera <12.14表單數據被支持,但它是錯誤的。

  • Safari將返回文件大小0

    Opera不支持表單數據的追加方法

    firefox <6無法正常工作

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

您需要檢查瀏覽器是否支持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!');
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM