我正在尝试使用这两个jQuery插件:plupload和jQuery Form Plugin 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 file.name做这个,我可以看到文件名,如果我发出警报但我不能发送它:
使用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 value1 =“dynamic_value1”替换这个var,它就可以了。所以我想,我的问题是关于这个特殊的var file.name
答案 0 :(得分:1)
也许您应该省略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);
}
});
});