简体   繁体   English

使用jQuery和Ajax上传文件

[英]Upload file using jQuery and Ajax

I have a form that I am sending to the server using jQuery $.post method. 我有使用jQuery $.post方法发送到服务器的表单。 I am sending several fields with something like: 我正在发送一些类似的字段:

var myName = "Pedro";
var myDescription = "Description of Pedro";

$.post("~/insert_user", { name: myName, description: myDescription }, function(data){
    alert("Successfully inserted " + data.name + " in DB");
}, "json");

This works great, as I take the values in insert_user.php and treat and insert the in the data base. 这很有效,因为我将insert_user.php的值用作数据并将其插入数据库中。

What if I need to add a field to my form to let the user upload an image? 如果我需要在表单中添加字段以允许用户上传图片怎么办?

How would I do that in my $.post call and how would I get it in the PHP? 我如何在$.post调用中做到这一点,如何在PHP中获得它?

I am using Symfony2.0, by the way, just in case it changes something. 顺便说一下,我正在使用Symfony2.0,以防万一它改变了某些东西。 But the important part is how to add a file typed field to the ajax call. 但是重要的部分是如何向ajax调用添加文件类型字段。

Thanks in advance. 提前致谢。

Pedro 佩德罗

You will find it would be a lot easier to use a pre built jquery plugin. 您会发现使用预制的jquery插件会容易得多。 My favourite is uploadify http://uploadify.com . 我最喜欢的是uploadify http://uploadify.com

Its simple and easy to use and it will save you a lot of time trying to figure out a method of your own <> 它简单易用,可为您节省大量时间,以找出自己的方法<>

$.post is the same as the code block bellow. $ .post与下面的代码块相同。 In order for you to do what you need to you need to change it to use this atleast and then things will become simpler. 为了使您能够做所需的事情,您需要对其进行更改以使用此至少特性,然后事情会变得更加简单。 So start by changing the $.post to this 因此,首先将$ .post更改为此

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

Then add a parameter in the ajax block contentType : "multipart/form-data" or try mimeType (cant remember so clearly) and in the data : $("#form").serialize(), that should work. 然后在ajax块contentType中添加一个参数:“ multipart / form-data”或尝试mimeType(请记住清楚)并在数据$(“#form”)。serialize()中运行。

please tell me if it didn't work 请告诉我它是否无效

--NEW EDIT LOL-- Excuse my blind coding, you may need to test this --NEW EDIT LOL--打扰一下,您可能需要测试一下

I did a bit more research and came across this. 我做了一些研究,发现了这一点。 You need to build this array and add it to the data in your ajax block 您需要构建此数组并将其添加到ajax块中的数据中

var files = new FormData($('#fileinputid'));     
jQuery.each($('#fileinputid')[0].files, function(i, file) {
    files.append(i, file);
});

If what i read was accurate you should be able to pass that array with the rest of the ajax data. 如果我读的是正确的,那么您应该能够将该数组与其余的ajax数据一起传递。 Although i am not completely sure how to add it to the serialized data. 虽然我不确定如何将其添加到序列化数据中。

Please test this and let me know. 请对此进行测试,并让我知道。 if it doesn't work ill do a full javascript script test for you and then post it here 如果它不能正常工作,请为您做一个完整的javascript脚本测试,然后在此处发布

本教程可能会有所帮助: Nettuts +:使用AJAX上传文件

Make iframe and put form in it, submit the form and voila. 制作iframe并将表单放入其中,提交表单并确认。 Or you can use one of these AJAX File upload scripts 或者,您可以使用以下AJAX文件上传脚本之一

I finally used this: 我终于用了这个:

http://malsup.com/jquery/form/#faq http://malsup.com/jquery/form/#faq

It just works great for what I need. 它可以满足我的需求。

Thank you guys for your help. 谢谢你们的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM