[英]jQuery File Upload, specifying FormData
我正在使用以下jQuery文件上傳插件:
https://github.com/blueimp/jQuery-File-Upload/wiki/Options
我需要特定的額外formdata,它說有一個選項,但我得到一個JS錯誤“Uncaught SyntaxError:Unexpected identifier”,並且沒有FormData示例,這使得很難開始工作。
這是我有的:
$(function () {
$('.upload').fileUploadUI({
uploadTable: $('.upload_files'),
downloadTable: $('.download_files'),
buildUploadRow: function (files, index) {
var file = files[index];
return $(
'<tr>' +
'<td>' + file.name + '<\/td>' +
'<td class="file_upload_progress"><div><\/div><\/td>' +
'<td class="file_upload_cancel">' +
'<div class="ui-state-default ui-corner-all ui-state-hover" title="Cancel">' +
'<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
'<\/div>' +
'<\/td>' +
'<\/tr>'
);
},
buildDownloadRow: function (file) {
return $(
'<tr><td>' + file.name + ' ' + file.type + ' ' + file.size + '<\/td><\/tr>'
);
},
formData:
[
{
name: '_http_accept'
value: 'application/javascript'
},
{
name: '<%= session_key_name %>'
value: encodeURIComponent('<%= u cookies[session_key_name] %>'),
},
{
name: 'authenticity_token'
value: encodeURIComponent('<%= u form_authenticity_token %>')
}
]
});
});
你沒有在formData
中的正確位置使用逗號,我想你希望它是這樣的:
formData: [
{
name: '_http_accept',
value: 'application/javascript'
}, {
name: '<%= session_key_name %>',
value: encodeURIComponent('<%= u cookies[session_key_name] %>')
}, {
name: 'authenticity_token',
value: encodeURIComponent('<%= u form_authenticity_token %>')
}
]
請注意, name: ...
后面有逗號name: ...
部分但不是value: ...
部分。
另外,我認為encodeURIComponent()
不是這里適當的轉義/編碼機制, <%= u ...
已經是URI編碼。 你需要做的就是確保字符串不包含未轉義的單引號,所以更像這樣的東西可能會起作用(假設這個JavaScript正在通過ERB):
value: '<%= cookies[session_key_name].gsub(/'/, "\'") %>'
適當的編碼應該由插件處理,並且幾乎肯定會進行POST,因此URL編碼甚至不適用。
此外,你不需要在JavaScript字符串中轉義斜杠,它們並不特別,所以你可以在'</td>'
中說'<\\/td>'
。
我對jQuery-File-Upload一無所知,但修復你的逗號至少應該讓你超越你的直接問題(以及新的和更有趣的問題!)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.