我已经阅读了如何提交额外的表单数据,它实际上是有效的。但我试图实现的是,每次发送区块时都更新formData,这样新数据就会与区块一起发送到服务器。
这就是我尝试过的:
$('#upload').fileupload({
maxChunkSize: 100000, // 100KB
formData: {UploadID: 'just testing'},
}).bind('fileuploadchunkdone', function (e, data) {
UId = data.jqXHR.responseJSON.files[0].UploadId;
console.log(UId); // Confirmed, it does have the data sent back by the server
data.formData = {UploadID: UId}; // It updates, but doesn't send the new data
}).bind('fileuploadchunksend', function (e, data) {
// tried the same here, but no luck
});
使用Firebug,我可以看到发送到服务器的数据。第一个区块正确发送formData,但第二个区块再次发送相同的数据。
我也尝试了作者在这里描述的内容,但重写send
处理程序不起作用,因为它只发生在第一个块上。
你知道我怎样才能做到这一点吗?
尚未对此进行测试,但您可以在更大的范围内定义UId
变量,并在每次调用时更新它:
var UId = 'just testing';
$('#upload').fileupload({
maxChunkSize: 100000, // 100KB
formData: function(){
return [{UploadID: UId}];
}
}).bind('fileuploadchunkdone', function (e, data) {
UId = data.jqXHR.responseJSON.files[0].UploadId;
});
不确定这是否是实现这一目标的正确方法。但我做的是:
var UId = 'just testing';
$('#upload').fileupload({
maxChunkSize: 100000, // 100KB
//formData: {UploadID: UId} REMOVED
}).bind('fileuploadchunkdone', function (e, data) {
// UId is set after the chunk is done
UId = data.jqXHR.responseJSON.files[0].UploadId;
}).bind('fileuploadchunksend', function (e, data) {
// and append UploadId
// Yes, append, not override
data.data.append('UploadId', UId);
// This will also be fired on the first chunk upload
// so it is better to set UId in a greater scope as koala_dev said
}
正如我所说,我不知道这是否正确。但它奏效了。
请注意,我已经从初始设置中删除了formData: {UploadID: UId}
。保持它在那里会使每个块发送两次UploadID
,首先在块的顶部(在文件数据之前)发送一个不可更改的值,然后在块的底部(在文件文件数据之后)发送新值。