我使用jQuery的ajax函数将文件发送到服务器端PHP脚本时遇到问题.可以获取文件列表$('#fileinput').attr('files')但是如何将此数据发送到服务器?使用文件输入时$_POST,服务器端php脚本上的结果array()为0(NULL).
我知道这是可能的(虽然我到目前为止还没有找到任何jQuery解决方案,只有Prototye代码(http://webreflection.blogspot.com/2009/03/safari-4-multiple-upload-with-progress.html) ).
这似乎是相对较新的,所以请不要通过XHR/Ajax提及文件上传,因为它肯定有用.
我需要Safari 5中的功能,FF和Chrome会很好但不是必需的.
我现在的代码是:
$.ajax({
url: 'php/upload.php',
data: $('#file').attr('files'),
cache: false,
contentType: 'multipart/form-data',
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud) 我正在使用此脚本上传我的图像文件:http://jsfiddle.net/eHmSr/
$('.uploader input:file').on('change', function() {
$this = $(this);
$('.alert').remove();
$.each($this[0].files, function(key, file) {
$('.files').append('<li>' + file.name + '</li>');
data = new FormData();
data.append(file.name, file);
$.ajax({
url: $('.uploader').attr('action'),
type: 'POST',
dataType: 'json',
data: data
});
});
});
Run Code Online (Sandbox Code Playgroud)
但是当我点击上传按钮时,JavaScript控制台会返回以下错误:
Uncaught TypeError: Illegal invocation
Run Code Online (Sandbox Code Playgroud)

你能帮助我吗?