试图学习如何使用新的AJAX文件上传,而不是使用iframe或良好的旧PHP仅上传文件。我了解XHR请求的工作原理,并长期一直使用jQuery的$.post
。但我无法得到这个。
原因:当我发布的数据(文件,我要上传),我只能通过$_POST
全球访问它,而不是需要$_FILES
全球。下面是一些我的代码真正快速:
<input type="file" id="file"/>
<input type="submit" id="submit" value="Upload" />
$("#file").on("change",function() {
var file = this.files[0];
}
$("#submit").click(function() {
var formData = new FormData();
formData.append('file',file);
$.ajax({
url: '<?php echo BASE_URL; ?>ajax/upload.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: formData,
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
});
在PHP脚本,当我var_dump $_POST
,我得到的文件,但是当我var_dump $_FILES
那里什么也没有。我发现这些资源,但他们似乎并没有为我工作:填充
- AJAX File Upload with PHP, HTML5 File API and jQuery
- jQuery AJAX file upload PHP
- jQuery AJAX file upload
我编辑了标题来形容您的具体问题。使用非常具体的标题有助于更多关注这个问题。我已经改进了代码块的格式,列出了资源(中间用'/'不太清楚),还有大写的缩写。 – Harry