weixin_33695082 2014-08-13 03:43 采纳率: 0%
浏览 38

jQuery,Ajax发布文件上传

I need to post the file upload to the current page by jquery, then upload the file , and then insert file upload, but the code not work... jquery can't post the file upload to the form:

 <form class="form-inline" role="form" id="form" enctype="multipart/form-data">
 <td colspan="3"><input type="file" name="pic_map" id="pic_map"></td>
 </form>

and this is javascript:

 $('#save').click(function (e) {
    $.post(
            "index.php?page=form1_sub1&pguid=<?php echo $pguid;?>",
            $("#form").serialize()
        )
        .done(function (data) {
            console.log(data);
            window.open('index.php?page=form1_sub2&pguid=<?php echo $pguid;?>', '_self');
        })
        .fail(function () {
            alert('<?php echo _CONNECTION_ERROR; ?>');
        });
});

and this is php code:

 $files = $_FILES['pic_map']['name'];
if(!$db->upload($_FILES['pic_map'], 'uploads/maps/'.$files)) {
    exit('file error');
}
  • 写回答

1条回答 默认 最新

  • weixin_33728268 2014-08-13 04:26
    关注

    Using XHR2, File upload through AJAX is supported. E.g. through FormData object, but unfortunately it is not supported by all/old browsers.

    FormData support:

    • IE 10+
    • Firefox 4.0+
    • Chrome 7+
    • Safari 5+
    • Opera 12+

    FormData Example:

    Markup:

    <form class="form-inline" role="form" id="form" enctype="multipart/form-data"  onsubmit="return submitForm();">
     <td colspan="3"><input type="file" name="pic_map" id="pic_map"></td>
     <td colspan="3"><input type="submit" name="save" id="save"></td>
     </form>
    

    Script:

    function submitForm() {
            console.log("submit event");
            var fd = new FormData(document.getElementById("pic_map"));
            $.ajax({
              url: "upload.php",
              type: "POST",
              data: fd,
              enctype: 'multipart/form-data',
              processData: false,  // tell jQuery not to process the data
              contentType: false   // tell jQuery not to set contentType
            }).done(function( data ) {
                console.log("PHP Output:");
                console.log( data );
            });
            return false;
        }
    

    Note: Can't send file using $("#form").serialize()

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)