[英]Problems with image upload from browsers to Amazon S3
最近,我已經實現了將圖像從瀏覽器中的javascript上傳到Amazon S3。 我只專注於FireFox,Chrome和IE。
最后,我還沒有為IE實現它-任何版本(甚至10)。
用例是這樣的:
另一個重要的事情是瀏覽器的位置協議是https。
問題:我遇到了幾個問題,主要是在IE上。
IE 10可以做到這一點(1和2),但是xhr.open(“ POST”,url,true)出現問題-當url以http://開頭時,崩潰(瀏覽器從https:// something調用它) .com / more / stuff )
3.1。 如果瀏覽器在http上,則可以使用。
3.2。 如果對以https://開頭的URL調用POST請求,它不會崩潰,但無法上傳3.2.1。 在FireFox上發生與3.2相同的情況
不可接受的解決方案:
如果該解決方案將支持較舊的瀏覽器,那將非常非常好;如果它支持IE9 +,那將是非常好的;如果僅支持IE10,這將是非常好的。 當然FF和Chrome也應該使用它。
這是一部分有效的代碼,但存在問題(問題3):
function sendFile(putFields){
var xhr = new getXHRObject(); // returns new XMLHttpRequest object.
var url = 'http://'+putFields.bucket+"."+putFields.host+"/";
xhr.onreadystatechange=function(){
if (http.readyState==4&&null!=http.status&&(http.status==200||http.status==201
||http.status==202||http.status==204||http.status==205||http.status==0)){
alert('success!');
}else if (http.readyState==4){
alert('fail ');
}
}
var params={
'acl':""+putFields.acl,
'key':""+putFields.key,
'Content-Type':putFields.contentType,
'AWSAccessKeyId':putFields.awsAccessKeyId,
'signature':putFields.signature,//encodeURIComponent()
'policy':""+putFields.policy,
'file':aFile
};
var fData = new FormData();
for (var p in params)
fData.append(p,params[p]);
xhr.open("POST", postUrl, true); // IE crashes here when https-->http is the case
setTimeout(function(){ xhr.send(fData);}, 100);
}
我正在嘗試尋找解決方案一段時間,但沒有真正的成功,請幫助!
如果仍然有人遇到此錯誤,我發現添加“ contentType”可以解決該問題
$('.uploadAssets').ajaxForm( {
dataType: 'json',
contentType: 'application/json; charset=utf-8',//added this line and problem solved
beforeSend: beforeuploadAssetsPostForm,
success: uploadAssetsFormResponse,
error: uploadAssetsError
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.