duanli0119 2016-06-08 20:26
浏览 57

JQuery multipart / data ajax发布

I am using JQuery to post form data and I have added this into my function to allow it to post / upload files:

mimeType:"multipart/form-data",

I am calling it in my HTML form here:

<form id="form1" method="post" action="/tickets/record?type=<?php echo $_GET["type"]; ?>&seq=<?php echo $_GET["seq"]; ?>" enctype="multipart/form-data" onsubmit="post_form('#form1');">

and trying to handle the attachments in PHP with:

$attachment_array = array();    
foreach($_FILES['ticket_update_files']['name'] as $key => $value) {
    if(!$_FILES['ticket_update_files']['error'][$key]) {

    } 
}

but its not recognising that any files that have been selected.

My full jquery function is:

function post_form(form_id, type, redir_url, loading_modal) {
    type = type || '';
    redir_url = redir_url || '';
    loading_modal = loading_modal || '';

    $( form_id ).submit(function(e) {
        var formObj = $(this);
        var formURL = formObj.attr("action");
        var formData = new FormData(this);

        CheckRequired(e);

        if(loading_modal === '1') { } else {
            LoadModalBody('<h2 align="center">Loading...</h3><p align="center"><i class="fa fa-refresh fa-spin fa-5x"></i></p>', 'Loading');
        }

        $.ajax({
            url : '/section' + formURL,
            type: "POST",
            data : formData,
            mimeType:"multipart/form-data",
            contentType: false,
            cache: false,
            processData:false,
            success:function(data, textStatus, jqXHR) {
                //alert(type);
                if(type === 'modal') {
                    if(redir_url === '') {
                        LoadModal('/section' + formURL, '');
                    } else {
                        LoadModal('/section' + redir_url, '');
                    }
                } else if(type === 'reload') {
                    if(redir_url === '') {
                        location.reload();
                    } else {
                        OpenPage(redir_url);
                    }
                } else {
                    //close the loading modal
                    if(loading_modal === '1') { } else {
                        CloseModal();
                    }
                    //location.reload();
                    //$("body").html(data);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                //if fails
            }
        });
        return false;
        e.preventDefault();
    });
}
  • 写回答

3条回答 默认 最新

  • dscrb00804 2016-06-19 11:44
    关注

    Try to add each file to FormData object manually. Here is how.

    HTML:

    <form id="my_form" method="post" action="" enctype="multipart/form-data">
        <input type="file" id="my_files" multiple>
        <input type="submit">
    </form>
    

    js:

    $( "#my_form" ).submit(function(e) {
        e.preventDefault();
        var data = new FormData();
        $.each( $('#my_files')[0].files, function(i, file) {
            data.append('file[]', file);
        });
        $.ajax({
            url: 'http://162.243.221.224/multipart/upload.php', // I will keep this script alive for few weeks
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            // mimeType:"multipart/form-data",
            type: 'POST',
            dataType: "text",
            success: function(data){
                alert(data);
            },
            error: function(data){
                alert(data);
            }
      });
        return false;        
    });
    

    PHP:

    <?php
        print_r( $_FILES['file']['name'] ); 
    

    Have tested it in Firefox 47.0 with latest jquery. Worked for me (without specifying mimeType in ajax and action attribute in form tag).

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端