Didn"t forge 2019-08-09 14:31 采纳率: 25%
浏览 68

通过AJAX发布Excel文件

Is there any way to send an Excel file through an AJAX POST request? I've tried this, but I don't know how to wrap the file through the request. Can AJAX do this?

<form id='file-import' method="POST" enctype="multipart/form-data">
  <meta name="csrf-token" content="{{ csrf_token() }}" />
  <label>Choose File</label>
  <div class="form-group">
    <input type="file" name="file" required="required">
  </div>
  <button type="submit" class="">Import</button>
</form>
$(document).ready(function() {
  console.log('run');
  $('#file-import').submit(function(e) {
    e.preventDefault();
    let form_data = new FormData($(this)[0]);
    console.log(form_data);
    $('#content').hide();
    $('#page-loader').fadeIn();
    $.ajax({
      url: '/test/post',
      type: 'POST',
      data: 'form_data',
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      },
      dataType: 'json',
      success: function(data) {
        console.log('success');
      },
      error: function() {
        console.log('error');
      }
    });
    $('#page-loader').fadeOut();
    $('#content').show();
  });
});
  • 写回答

1条回答 默认 最新

  • weixin_33728268 2019-08-09 14:34
    关注

    You have two main problems with this AJAX request. Firstly you're sending the string literal 'form_data', not the actual value held in the form_data variable. You need to remove the quotes around it.

    Secondly, you need to add the contentType and processData properties to the AJAX request when sending a FormData object, and both their values should be false.

    Also note that you need to call fadeOut() and show() when the AJAX request finishes. Where you have those calls right now means that you show the loader and then immediately hide it as the request is async. To make this work properly, move the calls in to a complete handler in the AJAX settings. Try this:

    $('#file-import').submit(function(e) {
      e.preventDefault();
      let form_data = new FormData($(this)[0]);
      var $content = $('#content').hide();
      var $pageLoader = $('#page-loader').fadeIn();
    
      $.ajax({
        url: '/test/post',
        type: 'POST',
        data: form_data,
        contentType: false,
        processData: false,
        headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        dataType: 'json',
        success: function(data) {
          console.log('success');
        },
        error: function() {
          console.log('error');
        },
        complete: function() {
          $content.show();
          $pageLoader.fadeOut();
        }
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?