php jQuery - 使用 $.post 接收 $_FILES 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/551627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - receiving the $_FILES array using $.post
提问by user49226
I am attempting to submit a form via jQuery. My form contains fields and a file that must be uploaded. It is of type ENCTYPE="multipart/form-data".
我正在尝试通过 jQuery 提交表单。我的表单包含必须上传的字段和文件。它的类型为ENCTYPE="multipart/form-data"。
I can receive all my field values using: post = $('#myForm').serialize();But how do I receive the $_FILESarray? I need this to process the uploaded file.
我可以使用以下方法接收所有字段值:post = $('#myForm').serialize();但是如何接收$_FILES数组?我需要这个来处理上传的文件。
Is this possible using jQuery, and if so how? Or do I need to use a special upload plugin for jQuery?
这是否可以使用 jQuery,如果可以,如何实现?或者我需要为 jQuery 使用特殊的上传插件吗?
回答by Bruce Aldridge
jquery form is the best way to do it, you can add it to any normal form,
jquery 表单是最好的方法,你可以将它添加到任何普通表单中,
<form method="post" action="URL">
<input type="file" name="file">
<input type="text" name"text">
<input type="submit">
</form>
<script type="text/javascript">
$(document).ready(function() {
$(form).ajaxForm();
})
</script>
will work as expected, but with ajax.
将按预期工作,但使用 ajax。
回答by Paolo Bergantino
You cannot upload files through javascript.
您不能通过 javascript 上传文件。
Check out this related question:
Is it possible to use Ajax to do file upload?
查看这个相关问题:
Is it possible to use Ajax to do file upload?
Essentially, the two most popular ways of "faking" AJAX for file uploads is using a Flash plugin such as SWFUploador submitting the form to a hidden iframe that processes the request.
本质上,两种最流行的“伪造”AJAX 文件上传方式是使用 Flash 插件(例如SWFUpload)或将表单提交到处理请求的隐藏 iframe。
回答by ajay
Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form. The file will not be sent
表单包含一个文件输入,但在表单上缺少 method=POST 和 enctype=multipart/form-data。文件不会被发送
回答by Luis Neighbur
Use FormData
使用表单数据
<form>
<label for="imageToSend">Cargar imagen a la galeria</label>
<input type="file" name="imageToSend" id="imageToSend" value="Cargar imagen" />
</form>
<script>
$('#imageToSend').on('change',function(event){
var dialog = $('#dialog');
var Data = new FormData();
Data.append('imageToSend',$('#imageToSend')[0].files);
$(this).val('');//Clear input file
$.ajax({
url: "/upload",
data: Data,
processData: false,
contentType: false,
type:'POST',
success: function(data){
if(data.success){
//success handler
}else if(!data.success){
//error backend handler
}
},
error: function(data){
//error handler Ej:404 status
}
})
});
</script>
回答by Ionu? G. Stan
If you can control the environment, like, say, you're writing an admin app for an intranet in which you recommend the browser, then real AJAX file uploads are possible with Firefox 3 and above. In all other cases, the iframe workaround or a Flash based uploader is the way to go.
如果您可以控制环境,例如,您正在为内部网编写管理应用程序并在其中推荐浏览器,那么使用 Firefox 3 及更高版本就可以上传真正的 AJAX 文件。在所有其他情况下,iframe 解决方法或基于 Flash 的上传器是可行的方法。
回答by Ionu? G. Stan
It's possible, but not working in Google Chrome ) Look!
这是可能的,但不能在谷歌浏览器中工作)看!
...
<form method='post' enctype='multipart/form-data'>
<input type="file" id="imf" name="imf"/>
<input type="button" id="Save"/>
</form>
...
$("#Save").live("click", function(){
var photo = document.getElementById("imf");
var file = photo.files[0];
$.post('/user/saveNewPhoto', {'imf':file.getAsDataURL(), fname:file.fileName }, function( data ){
alert ( data );
});
});
upload side script need decode base64 ) and that is all but i don't test this script on big size file
上传端脚本需要解码 base64 ),仅此而已,但我不会在大文件上测试此脚本

