我有一個網頁,獲取公共S3存儲桶的名稱和唯一的文件名用於上傳。我正在使用jQuery從javascript進行上傳。文件上傳,但多部分信息預先添加到損壞它的文件。jQuery上傳到公共S3桶提供------ WebKitFormBoundary
我已經嘗試了不同的jQuery ajax參數,如製作contentType'image/jpeg'並嘗試使用PUSH而不是PUT。我甚至嘗試手動將文件添加到新的FormData()對象,如下所示。
我不明白我怎麼可以寫一個簡單的CURL命令,它沒有問題,但我無法獲得使用jQuery提交的HTML表單工作。我覺得像jQuery一樣,它被卡住,並沒有正確地放在S3存儲桶一側。
HTML
<form id="upload-image-form" className="clearfix" method="PUT" enctype="multipart/form-data">
<input id="product-images-add-btn" name="product-images-add-btn" type="file" onChange={this.startImageUpload} />
</form>
的Javascript
$("#upload-image-form").submit(function(e)
{
e.preventDefault();
var formURL = $(this).attr('action');
//var formData = new FormData($("#product-images-add-btn")[0]);
var formData = new FormData();
formData.append("file", $("#product-images-add-btn")[0].files[0]);
$.ajax({
url: formURL,
type: "PUT",
data: formData,
crossDomain: true,
contentType: false,
processData: false,
success: function(data, textStatus, jqXHR)
{
console.log('Successful image upload!');
//tempThis.finishImageS3Upload();
},
error: function(jqXHR, textStatus, errorThrown)
{
console.error("Image upload error.");
}
});
return false;
});
startImageUpload: function(){
var input = $("#product-images-add-btn");
var files = input[0].files;
var fileTypes = [".gif",".jpg", ".png"];
console.log("--- DEBUG. Number of files: "+files.length);
var tempThis = this;
if(files.length > 0)
{
var ext = input.val().match(/\.([^\.]+)$/)[0];
if(fileTypes.indexOf(ext) > -1)
{
var file = files[0];
var assetData = {
"filename": file.name,
"filesize": file.size,
"type": "IMAGE",
"format": file.type
};
$.ajax({
url: "/getImageUploadS3Info",
cache: false,
contentType: 'application/json',
crossDomain: true,
data: assetData,
success: function(s3Info){
s3Info.filename = file.name;
tempThis.uploadImageS3(s3Info);
},
error: function(a, x, e){
console.error("getS3InfoForUpload() ajax error.");
}
});
}
else
console.warn("--- DEBUG. File extension "+ext+" not allowed.");
}
else
console.log("--- DEBUG. No files to upload.");
}
uploadImageS3: function(s3Info){
if(s3Info.hasOwnProperty("assetId"))
{
$("#upload-image-form").attr('action', s3Info.httpUrl)
.data('assetId', s3Info.assetId);
console.log("--- DEBUG: submitting form.");
$("#upload-image-form").submit();
}
else
console.warn("--- ERROR: the response for getImageUploadS3Info wasn't correct.");
}
S3文件
------WebKitFormBoundaryluTBbSMgcV0BEJcC
Content-Disposition: form-data; name="file"; filename="NewGrass.jpg"
Content-Type: image/jpeg
<image byte info stuff>
我面臨着這個完全相同的問題,你能解決它嗎? – dev123
不可以。我們最終回到將文件發送到我們的後端Java並將其上傳到S3 – Hososugi