JQuery1.8.0:通过AJAX从表单发送文件 [英] JQuery1.8.0: Sending files from forms via AJAX

查看:162
本文介绍了JQuery1.8.0:通过AJAX从表单发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用CakePHP开发,最初认为这个问题是蛋糕相关的;它不是。

I develop with CakePHP and initially thought this problem was Cake-related; it wasn't. I've re-written the question to make the answer I received more widely applicable

我有一个表单

  <form action"">
      <fieldset> 
      <!---- bunch of fields----->
      <input type="file" name="data[Upload][file]" id="UploadFile">
      <button id="ajaxUploadSubmit"> Submit </button>
      </fieldset>
  </form>

我写的submit函数看起来像:

And the submit function I've written looks like:

$( "#ajaxUploadSubmit" ).click(function() {
                $.ajax({
                    url:"uploads/add",
                    data: $( "#UploadsAddForm" ).serialize()
                }).done(function(responseText) {
                    alert(responseText);
                  })
                 .fail(function() {
                     alert('failxors');
                  })
                });

但是这一行返回一个空数组: $ this-& > data

But this line returns an empty array: $this->request->data.

推荐答案

我只添加了此答案,因此此问题可以标记为回答实际上thaJeztah在Jai的回答的第三个注释中回答这个问题:在这种情况下,问题是我应该使用FormData()而不是Serialize(),如下所示:
如何使用jquery序列化上传文件

I'm only adding this answer so this question can be marked as answered; in fact thaJeztah answered this in the 3rd comment of Jai's answer: the problem, in this case, was that I should have been using FormData() instead of Serialize() as seen here: how do to file upload using jquery serialization

这篇关于JQuery1.8.0:通过AJAX从表单发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆