douchi0028 2016-10-31 08:17
浏览 77

如何通过jQuery步骤上传图像

i know how to upload image by ajax but i want to upload image by jQuery steps. I've tried multiple ways but its not not working. if anyone ever done that please help me. Thank

HTML

<input type="file" style="background-color: #2184b3; color: #fff;" class="btn btn-default" name="upload_doc" id="upload_doc" title="Search for a file to add">

jQuery

if(currentIndex == 0)
                    { 
                        var upload_doc = $("#upload_doc").val();


                        event.preventDefault();
                        $.ajax({
                          async: false,
                          url: myUrl,
                          dataType: 'json',
                          type: 'POST',
                          cache: false,
                            contentType: false,
                            processData: false,
                          data : {  upload_doc : upload_doc, step1 : step1},
                          success: function(response) {
                            console.log(response);
                          }
  • 写回答

2条回答 默认 最新

  • douba9776 2016-10-31 08:42
    关注

    From your comment,

    actually the thing is that I'm submitting many values also when uploading the image. so one click of next i sends so many data including image. rest data goes well except image only.

    If you're uploading file, along with other input values, through AJAX, use FormData object. But keep in mind that old browsers don't support FormData object, FormData support starts from the following desktop browsers versions: IE 10+, Firefox 4.0+, Chrome 7+, Safari 5+, Opera 12+.

    So your jQuery should be like this:

    if(currentIndex == 0){
        event.preventDefault();
        var formData = new FormData($('form')[0]);
        $.ajax({
            async: false,
            url: myUrl,
            dataType: 'json',
            type: 'POST',
            cache: false,
            contentType: false,
            processData: false,
            data : formData,
            success: function(response) {
                console.log(response);
            }
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line