gpt4 book ai didi

javascript - Android 股票浏览器中没有 XMLHttpRequest 2 进展

转载 作者:太空狗 更新时间:2023-10-29 16:47:24 26 4
gpt4 key购买 nike

我有一个工作文件上传表单,它使用 XMLHttpRequest 2 将文件上传到 Transloadit(一种文件处理服务)。进度事件在 Firefox 和 Chrome 中正确触发,适用于桌面和 Android。但是 Android (4.0) 库存浏览器不会触发这些事件,因此我的进度条对我网站的那些用户不起作用。 XHR2 和 ProgressEvent 据说从 3.0 开始在 Android 中得到支持,所以我不知道发生了什么。

代码是:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Upload test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link href="/assets/application.css" media="all" rel="stylesheet" type="text/css" /> <!-- twitter bootstrap -->
<script type="text/javascript">

var onprogress = function(e)
{
var $status = $('.status');
$status.text('onprogress: ' + (e.loaded / e.total) * 75 + '<br>');

if (e.lengthComputable) {
setProgress((e.loaded / e.total) * 75);
}
}

var setProgress = function(percent) {
percent = percent+'%';
var $progressBar = $('.progress .bar');
var $status = $('.status');
$progressBar.css('width', percent);
$progressBar.text(percent);
$status.text(percent);
};

var createAttachment = function(file) {
var params = {"blocking": true,"auth":{"key":"XXX"},"template_id":"XXX"}
var xhr = new XMLHttpRequest();
var data = new FormData();

data.append('params', JSON.stringify(params));
data.append('attachment[file]', file);

var evtReceiver = xhr.upload || xhr;
evtReceiver.addEventListener('progress', onprogress, false);
xhr.onload = function(e) {
var resp = JSON.parse(e.target.response)
$('.image').append($('<img src="' + resp.results['android-thumb'][0].url + '">'))
setProgress(100);
};

xhr.open('POST', 'http://api2.transloadit.com/assemblies', true);
xhr.send(data);

};


$(document).ready(function() {
$('input[type=file]').on('change', function(event) {
createAttachment(event.target.files[0]);
});
});
</script>

</head>

<body>

<input type="file" name="my_file">

<div class="progress progress-striped active">
<div class="bar" style="width: 0;"></div>
</div>

<div class="status"></div>

<div class="image"></div>

</body>
</html>

最佳答案

Android 浏览器的 Chromium 版本支持进度事件:

Android 4.4 (API level 19) introduces a new version of WebView that is based on Chromium. This change upgrades WebView performance and standards support for HTML5, CSS3, and JavaScript to match the latest web browsers. Any apps using WebView will inherit these upgrades when running on Android 4.4 and higher.

升级到 4.4 以使用此 API。

引用资料

关于javascript - Android 股票浏览器中没有 XMLHttpRequest 2 进展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13503361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com