我使用Ajax调用要上传的文件,通过PHP处理。该文件应被放置在根据一个jquery变量的特定目录。我可以上传文件,但随着文件不能传递变量。 PHP报告一个未定义的索引错误。
I'm using an ajax call to upload a file, handled by PHP. The file should be placed in a specific directory based on a jquery variable. I can get the file to upload, but cannot pass the variable along with the file. PHP reports an undefined index error.
阿贾克斯code:
var fd = new FormData();
fd.append( 'file', document.getElementById('select').files[0]);
$.ajax({
url: 'test.php',
type: 'POST',
data: fd,
processData: false,
contentType: false,
success: function(e){
// some code here
}
});
我试图改变的数据属性设置为FD +'和; myVar的='+ myVar的,但是PHP不能正确地分析数据,并同时为$ _FILES ['文件']变量返回undefined指数的错误,以及在$ _ POST ['myVar的']变量。
I tried changing the data property to "fd+'&myVar='+myVar, however PHP cannot parse the data correctly and returns undefined index error for both the $_FILES['file'] variable as well as the $_POST['myVar'] variable.
我如何能同时发送的文件和一个变量?
How can I send both the file and a variable?
如果你需要另一种形式的字段,调用fd.append第二次:
If you need another form field, call fd.append a second time:
fd.append('file', document.getElementById('select').files[0]);
fd.append('myVar',myVar);