【问题标题】:file not uploaded to the server with ajax文件未使用 ajax 上传到服务器
【发布时间】:2014-04-20 22:21:09
【问题描述】:

这是我的表单来源:

<form id="createProject" name="createProject" class="form-light" method="post" enctype="multipart/form-data" target="upload_target">
    <div  class="col-md-10" id="sandbox-container1">
    <label>Project duration</label>
    <div class="input-daterange input-group" id="datepicker">
    <input type="text" required class="input-md form-control" id="start" name="start" />
    <span class="input-group-addon">to</span>
    <input type="text" required class="input-md form-control" id="end" name="end" />
    </div>
    </div>
    <div class="col-md-10">
    <div class="form-group">
    <label>Project attachment's</label>
    <input type="file" class="form-control" id="projectFile" name="projectFile" placeholder="Select project  attachment's">
    </div>
    </div>
    <input type="hidden" id="ownerid" name="ownerid" value="<?php echo $userid; ?>">
    <div class="col-md-10">
    <div class="form-group">
    <button class="btn btn-two pull-right btn-lg" form="createProject" type="submit">Submit</button>
    </div>
    </div>
</form>

文件未上传到服务器。
我使用ajax函数提交。

$.ajax({
    url:'ajaxcalls/createprojectfunction.php',
    data:$(this).serialize(),
    type:'POST',
    beforeSubmit:  function()
    {
        /* Before submit */
        for ( instance in CKEDITOR.instances )
        {
            CKEDITOR.instances[instance].updateElement();
        }
    },
    success:function(data){
        console.log(data);
    },
        error:function(data){
    }
});

Firebug 控制台出现此错误。
注意:未定义索引:projectFile.
但所有其他输入元素都可以在 ajax URL php 文件中访问。

【问题讨论】:

    标签: php jquery ajax file-upload


    【解决方案1】:

    您必须使用 FormData 对象通过 ajax 上传文件,

    $.ajax({
      url:'ajaxcalls/createprojectfunction.php',
      data: new FormData(this),
      type:'POST',
      processData: false,
      contentType: false,
      beforeSubmit:  function()
      {
            /* Before submit */
        for ( instance in CKEDITOR.instances )
        {
            CKEDITOR.instances[instance].updateElement();
        }
        },
      success:function(data){
      console.log(data);
      },
      error:function(data){
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多