【发布时间】:2013-06-26 18:20:04
【问题描述】:
我正在尝试使用 jquery 和 PHP 来上传这样的图像:
HTML
<form id="form-photo" enctype="multipart/form-data"><
<span class="btn btn-small fileinput-button">
<span>Add Photo</span>
<input type="file" name="photo-img" id="photo-img">
</span>
</form>
当前的 jquery 框架如下所示:
$('#photo-img').change(function(){
var file = this.files[0];
//console.log(file);
name = file.name;
size = file.size;
type = file.type;
// I'll do some validation here before proceeding
var formData = new FormData($('#form-photo'));
console.log(formData);
// How do I pass the image form data to a PHP function via jquery?
});
我可以通过 jquery post 将 formData 传递给 PHP 函数吗?
【问题讨论】:
-
只有现代浏览器才能处理通过 ajax 发送文件
-
看看这个用于处理 ajax 文件的 jQuery 插件:jquery.malsup.com/form
-
此时,您的表单与任何其他表单没有什么不同。看看Making an HTTP POST call with multipart/form-data using jQuery?还有jQuery .ajax() documenation