将$ _FILES变量传递给AJAX

时间:2015-11-20 09:04:03

标签: php html ajax

我有一个包含文件上传的表单,如下所示:

<form name="addform" id="addform" method="post" action="add.php?action=add" enctype="multipart/form-data">
File 1: <input type="file" name="file1" />
<input name="add" type="submit" value="ADD">
</form>

我想在$_FILES中传递AJAX变量,因为我使用了弹出对话框。这是我试着写的AJAX

$.ajax({
    type: 'POST',
    url: 'addanduploadfile.php',
    data: ?,
    success: function(msg)
    { 
         alert(msg);    
    }
});

但我的问题是我不知道如何将$_FILES传递到AJAX并在file1上显示addanduploadfile.php名称。我该怎么办?

2 个答案:

答案 0 :(得分:0)

看看

  1. Multiple File Upload AJAX
  2. AJAX File upload
  3. 希望这有帮助。

答案 1 :(得分:-1)

试试这个,

var file_data = $('#addform').prop('files')[0];// collect files 
var form_data = new FormData();// take other form data
form_data.append('file', file_data);// append the files to your data

$.ajax({
     type: 'POST',
     url: 'addanduploadfile.php',
     data: form_data, // pass form data here
     success: function(msg)
     { 
            alert(msg);    
     }
 });