我正在尝试使用这两个jQuery插件:plupload和jQuery Form插件ajaxForm。
它工作得很好,除了一件事:我不能发送之前用plupload上传的文件的file.name(带ajaxForm)。
我稍微解释一下:用户发送一个带有plupload的文件。文件已上传。它运行良好。
然后,用户使用ajaxForm提交表单,并使用post方法发送表单数据+文件名。
我知道如何使用ajaxform发送数据,此代码运行良好:
var value1 = "dynamic_value1";
$('#my_form').ajaxForm({
// datas is sent in post method, it works fine
data: { value1: value1 },
beforeSubmit: validate,
success: function() {
// it's ok :
//alert(value1);
}
});
但我不能用pluplopad文件名做到这一点,如果我发出警报,我可以看到文件名,但我不能发送:
Plupload代码以获取文件名(有效):var file_name_vous;
uploader.bind('FileUploaded', function(up, file, response) {
// It's ok : i can get file name, alert show me the file name
file_name_vous = encodeURIComponent(file.name);
alert(file_name_vous);
//};
});
});
但我不能这样做,这个代码不起作用:
$participer_form.ajaxForm({
type: 'POST',
data: {
// impossible to send this var
file_name_vous: file_name_vous
},
beforeSubmit: validate,
// success
success: function() {
// It's ok, alert shows the file name
alert(file_name_vous);
}
});
所以我不明白的是,我可以用post方法发送数据,我已经测试过了。但我不能发送这个特定的var:file_name_vous=encodeURIComponent(file.name);
你知道在尝试用post方法发送之前,我是否应该对(file.name)做些什么吗?
我没有错误,只是在firebug networks/XHR中,我看不到任何关于这个var的信息。如果我用var value1="dynamic_value1"替换这个var,它就可以工作了。所以我想,我的问题是关于这个partulier var file.name
也许您应该省略ajax表单的数据部分,只需在成功上传后创建一个隐藏字段,该字段将与表单一起提交。
类似这样的东西:
uploader.bind('FileUploaded', function(up, file, response) {
// It's ok : i can get file name, alert show me the file name
file_name_vous = encodeURIComponent(file.name);
// maybe you'll have to check if hidden filed already exists
$participer_form.Append($('<input type="hidden" value="'+file_name_vous+'" id="file_name_vous" name="file_name_vous"/>'));
//};
希望这将有助于
顺便说一句,你有没有尝试过,尽可能晚地设置值?
uploader.bind('FileUploaded', function(up, file, response) {
// It's ok : i can get file name, alert show me the file name
file_name_vous = encodeURIComponent(file.name);
$participer_form.ajaxForm({
type: 'POST',
data: {
// impossible to send this var
file_name_vous: file_name_vous
},
beforeSubmit: validate,
// success
success: function() {
// It's ok, alert shows the file name
alert(file_name_vous);
}
});
});