我不能让这个脚本工作。每次我发送一个文件,脚本需要做一个ajax请求并发送到服务器,但是,serialize总是发送空字符串。
我的Javascript
$('#upload-button input:file').change(function() {
$('#upload-text').text('Sending file...');
$.ajax({
type: "GET",
url: "?url=images/send",
data: $('#image-upload').serialize(),
success: function(response) {
if(response.error) {
$('#upload-text').text(response.error);
} else if(response.success) {
$('#upload-text').text('Image send');
}
},
});
})我的HTML
<form name="image-upload" id="image-upload" action="?url=imagens/enviar" method="post" enctype="multipart/form-data">
<input type="file" name="selected-image" id="upload-hidden" value="" />
</form>当我选择了一个文件时,结果来自alert($('#image-upload').serialize());
// EMPTY //来自alert($('#image-upload input:file').attr('value'))的结果
ponte-vecchio.jpg发布于 2012-04-22 03:03:38
使用jQuery form plugin。它支持HTML5文件上传,如果浏览器太旧,可以退回到隐藏的iframe。
发布于 2012-04-22 02:54:59
如果你想提交文件,那么你必须使用iframe-“变通办法”描述的here,例如
对于您的问题,为什么serialize总是空的请看一下jquery的源代码。输入字段的类型将根据此正则表达式进行测试
/^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i正如您所看到的,"file“类型不是其中的一部分,因此jquery不会序列化这种输入字段。
发布于 2012-04-22 02:55:17
检出此fiddle。它通过jQuery设置基本的html和JS来使用通过iframe上传。“异步”上传文件的好方法;)
https://stackoverflow.com/questions/10261888
复制相似问题