weixin_33724659 2013-01-16 18:15 采纳率: 0%
浏览 80

异步上传ajax文件

I am trying to upload a text file to a python script. If I just upload form data and use

form_contents = $(this).serialize() + "&async=true"; 

then the form will stay on the web page and display the results. If I use

var formData = new FormData($('form')[0]);

it submits the form and data file but it doesn't stay on the web page. How do I add + &async=true to get it to stay on the web page. Or is there a different way to do this?

Stays on web page but doesn't upload file:

          $('#upload').submit(function () {
      form_contents = $(this).serialize() + "&async=true";
      form_action = $(this).attr('action');

      $.ajax({
      type: 'post',
          data: formData,
      url: form_action,
      success: function (result) {
      $('#upload').html(result);
      } 
      });
      return false;
  });

Doesn't stay on web page but uploads file:

$('#upload').submit(function () {
    var formData = new FormData($('form')[0]);
    form_action = $(this).attr('action');

    $.ajax({
    type: 'post',
    xhr: function() {
        myXhr = $.ajaxSettings.xhr();
        if(myXhr.upload){ 
        myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
    }
    return myXhr;
    },
    data: formData,
    url: form_action,
    success: function (result) {
        $('#upload').html(result);
    }
    });
    return false;
    });

Thanks for your help,

Matt

  • 写回答

2条回答 默认 最新

  • weixin_33725270 2013-01-16 18:23
    关注

    You need to also set the contentType and processData parameters to false.

    $('#upload').submit(function () {
      var formData = new FormData($('form')[0]);
      form_action = $(this).attr('action');
    
      $.ajax({
        type: 'post',
        xhr: function () {
          myXhr = $.ajaxSettings.xhr();
          if (myXhr.upload) {
            myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
          }
          return myXhr;
        },
        data: formData,
        url: form_action,
        processData: false,
        contentType: false,
        success: function (result) {
          $('#upload').html(result);
        }
      });
      return false;
    });
    

    however, if an error occurs, the form will continue to submit because it never reaches return false;. to stop that so you can see the error, use event.preventDefault.

    $('#upload').submit(function (e) {
      e.preventDefault();
      var formData = new FormData($('form')[0]);
      form_action = $(this).attr('action');
    
      $.ajax({
        type: 'post',
        xhr: function () {
          myXhr = $.ajaxSettings.xhr();
          if (myXhr.upload) {
            myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
          }
          return myXhr;
        },
        data: formData,
        url: form_action,
        processData: false,
        contentType: false,
        success: function (result) {
          $('#upload').html(result);
        }
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调