weixin_33734785 2018-01-02 10:01 采纳率: 0%
浏览 133

Framework 7文件上传Ajax

i am trying to upload file with ajax in Framework 7.

the code was made to post a form but requirement changed and added a file upload with it .

i tried to modify existing code with no luck .

it throws a error when i attached a file with new FormData() i tried to implement several methods to attach file but sadly none of them are working .

if anyone help me to sort problem

Uncaught TypeError: Illegal invocation

function addVoiceMail() {
  var status = '0'; 
  var formData = myApp.formToJSON('#my-form-sms');
  var data2 = JSON.stringify(formData);

  var fileInput = document.getElementById("msg_file");
  var post_data = new FormData();
  if (fileInput.files.length > 0) {
    post_data.append('file' , fileInput.files[0]);
  }
$.ajax({
  url: baseurl2 + 'somepage.php',
  type: "POST",
  data: {
    Data: data2,
    file: post_data,
  },
  crossDomain: true,
  cache: false,
  success: function (data) {
    if (data == 'success') {
      myApp.alert('sent', '');
      mainView.router.load({
        url: 'success.html',
        ignoreCache: true,
        animatePages: true
      });
    }else {
      var errors = JSON.parse(data);
      $.each(errors, function (key, value) {
        myApp.addNotification({
          title: 'Error',
          message: value
        });
      });
    }
  }
});

}

  • 写回答

1条回答 默认 最新

  • weixin_33676492 2018-01-08 11:23
    关注

    Your data object needs to contains only the FormData object. You have to add data2 directly in the FormData object.

    Try something like this :

    var formData = new FormData();
    formData.append('file', myFile); //append file to FormData
    formData.append('data', data2); //add additionnal informations
    

    Finally, pass your formData object to ajax data :

    $.ajax({ 
       data: formData,
       ...
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算