【问题标题】:Is it possible to send a file object in this way? [duplicate]是否可以以这种方式发送文件对象? [复制]
【发布时间】:2018-02-04 02:52:46
【问题描述】:

我是 jquery 的初学者。我正在尝试发送我从中获取的 File 对象 我的html:

 <label for="photo" class="col-xs-2">Photo</label>
 <input id="photo" name="fileName" type="file" class="col-xs-4"/>

这是我的 JavaScript:

 var file = $('#photo')[0].files[0];
 console.log(file);

 var data = { name: name, dob: dob,fname: fname,mob: mob,mName: mName, Email: Email, NID: NID,password: password,District: District,Thana: Thana,gender: gender,file: file };
 $.ajax({url: "singleUpload",
                type: "POST",
                data:data,
                contentType: false,
                success: function (response) 
                {
                $("#academic").html(response);
                console.log("ajax ok");
                }    
 });

我不想使用 new FormData() 或 iFrame。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:
     <form>
     <label for="photo" class="col-xs-2">Photo</label>
     <input id="photo" name="fileName" type="file" class="col-xs-4"/>
     </form>
    

    $('form').on('submit', uploadFiles);
    
    // Catch the form submit and upload the files
     function uploadFiles(event)
    {
      var file = $('#photo')[0].files[0];
        console.log(file);
        var Jsondata = { name: name, dob: dob,fname: fname,mob: mob,mName: mName, Email: Email, NID: NID,password: password,District: District,Thana: Thana,gender: gender,file: file }; 
        event.stopPropagation(); // Stop stuff happening
        event.preventDefault(); // Totally stop stuff happening
    
        // START A LOADING SPINNER HERE
    
        // Create a formdata object and add the files
        var data = new FormData();
        $.each(Jsondata , function(key, value)
        {
            data.append(key, value);
        }); 
    
         $.ajax({
                url: 'singleUpload',
                type: 'POST',
                data: data,
                cache: false,
                dataType: 'json',
                processData: false, // Don't process the files
                contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                success: function(data, textStatus, jqXHR)
                {
                    if(typeof data.error === 'undefined')
                    {
                        // Success so call function to process the form
                        submitForm(event, data);
                    }
                    else
                    {
                        // Handle errors here
                        console.log('ERRORS: ' + data.error);
                    }
                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    // Handle errors here
                    console.log('ERRORS: ' + textStatus);
                    // STOP LOADING SPINNER
                }
            });
        }
    

    【讨论】:

      【解决方案2】:
       var data = { name: name, dob: dob,fname: fname,mob: mob,mName: mName, Email: Email, NID: NID,password: password,District: District,Thana: Thana,gender: gender,file: file };
       $.ajax({url: "singleUpload",
                      type: "POST",
                      data:JSON.stringify(data),
                       dataType: 'json',
                      contentType: 'application/json; charset=utf-8',
                      success: function (response) 
                      {
                      $("#academic").html(response);
                      console.log("ajax ok");
                      }    
       });
      

      您可以使用此 data:JSON.stringify(data)

      传递对象

      【讨论】:

      • 谢谢回答。 FormData 对象似乎很有用。我已经通过 formdata.append() 方法完成了这项任务。我从 formdata.get() 方法获得了 formdata 值。顺便说一句,Spring 控制器真是太棒了。我通过@RequestParam(value =“keyname”)在spring控制器中获得了formdata键。由于我接受了一个答案,我无法发布我的解决方案。希望对某人有所帮助。
      猜你喜欢
      • 2016-02-02
      • 2016-03-09
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2016-04-09
      • 2016-11-28
      • 2019-10-20
      • 1970-01-01
      相关资源
      最近更新 更多