PHP jQuery .ajax()文件上传服务器端了解

我有一个以前只用于用户文本属性的HTML表单,这些表单都是使用AJAX通过URL调用PHP控制器函数来通过数据库和服务器端调用刷新页面内容(删节版本)

   

这个“创建用户”表单现在需要为用户图像合并文件上载机制

   

问题是表单是通过.ajax #create表单提交中的.serialize提交的

  $.ajax({ url: 'controller.php?command=create', type: 'POST', data: $( form ).serialize(), 

create URL调用PHP控制器echo $dmv->create(); ,这是模型公共function create(){ //execute database insert and execute

我已经读过序列化对文件没有意义,这是真的,但我也想弄清楚如何更新我现有的表单以将文件上传function合并到它

我试图理解jquery.form.js插件( http://malsup.com/jquery/form/#file-upload ),但无法理解如何将它们组合在一起。

我相信我需要做的是将文件上传作为单独的逻辑执行,然后与文件名的原始逻辑绑定,这是文件存储,其中唯一的图像名称存储在记录下的数据库中,而不是BLOB图像存储。

如果我能提供任何进一步的信息,请告诉我,并感谢您提供的任何帮助。

以下是迈克尔所谈论的一个例子。 http://www.phpletter.com/Our-Projects/AjaxFileUpload/

您无法通过AJAX上传文件。 您拥有的唯一可能是使用Flash(例如Uploadify: http ://www.uploadify.com/),iFrame,或者只是提交表单。 表单的enctype必须设置为multipart。

 

插件可以通过创建“隐藏”iframe来模仿AJAX文件上传。 示例: http : //valums.com/ajax-upload/

您可以使用隐藏的iframe模仿AJAX调用。 您甚至可以像AJAX调用一样实现回调函数并获取服务器响应:

HTML –

 

JS–

 //bind an event handler to the form with the file input $('form').on('submit', function () { //check to make sure the user has selected an image, if not then stop the form from submitting if ($('#userImage').val().length == 0) return false; //bind an event handler to the `load` event for the iframe so we will have a callback for the form submission $('#workFrame').on('load', function () { var $this = $(this); //remove this event handler $this.off('load'); //get the response from the server var response = $this.contents().find('body').html(); //you can now access the server response in the `response` variable }); //return true so the form submits normally return true; }); 

请注意, .on()在jQuery 1.7中是新的,在这种情况下与.bind()相同。