提交文件对象:非法调用
Posting file objects: illegal invocation
我想张贴多个图像对象到testphp.php。但是控制台打印错误说非法调用。
I have try:
<script type="text/javascript" src="/script/googleapis.js"></script>
<input multiple type="file" id="myFile" size="50">
<div id="sub">submit</div>
<div id="testtest"></div>
<script>
$("#sub").click(function(){
// get the file objects
var files = $("#myFile")[0].files;
for (var i = 0; i < files.length; i++){
//test if the files[i] has the file objects
console.log(files[i]);
//post objects to another php file
$.post("testphp.php", {img: files[i]}, function(result){
$("#testtest").html(result);
});
}
})
你不能那样做。如果你想发布文件,你需要使用FormData
.
我还建议一次上传所有文件,而不是一次上传一个。
要发布FormData
,您需要使用$.ajax
代替。
$("#sub").click(function(){
// get the file objects
var files = $("#myFile")[0].files,
data = new FormData;
for (var i = 0; i < files.length; i++){
//test if the files[i] has the file objects
console.log(files[i]);
//post objects to another php file
data.append('img[]', files[i]);
}
$.ajax({
url: "testphp.php",
type: "POST",
data: data,
contentType: false,
processData: false,
success: function(result){
$("#testtest").html(result);
}
});
});
现在在PHP中,如果您要上传多个文件,您的$_FILES['img']['name']
(和其他键)可能是数组。
相关文章:
- AJAX调用中的非法调用
- 如何解决这种情况下的非法调用类型错误
- 为什么从数组调用时收到非法调用
- 使用ajax和bootbox进行非法调用
- 未捕获的类型错误:非法调用:模态形式的错误
- 对间谍对象的非法调用
- 非法调用错误 jquery
- $.post投掷”;非法调用”;
- 调用module.exports上的函数时发生CommonJS非法调用错误
- 使用 Web 音频 API 使用 start/noteOn 进行非法调用
- 为什么这会引发未捕获的类型错误:非法调用
- jQuery ajax Uncatch TypeError: 非法调用
- 什么是非法调用类型JavaScript中的错误
- 在 jquery 中将错误作为非法调用
- 为什么某些函数调用在 JavaScript 中被称为“非法调用”
- JavaScript 中的 getUserMedia() 跨浏览器规范化.非法调用
- Array.prototype.forEach.call 给出 TypeError: 非法调用
- 未捕获的类型错误:发送数据时非法调用
- 未捕获的类型错误:从 ajax 发送数据时出现非法调用错误
- 尝试将javascript转换为jquery时出现错误“非法调用”