當前位置:首頁 » 文件管理 » 上傳圖片原型

上傳圖片原型

發布時間: 2023-07-15 13:22:23

㈠ 如何在前端用js進行多圖片上傳

    產品提了一個需求,要求在一個html中實現多行多圖片上傳,原型圖如下:

    2.1 :html

        html頁面由前端實現,此處增加<ul><li></li></ul>是為了配合圖片單擊放大圖片功能的實現

        <ul  id="ul_other">

              <li><input type="file" id="file_other" class="file_input" onchange="add_file_image('other')"></li>

        </ul>

    2.2 :js

        var imgSrc_other=[];

        var imgFile_other=[];

        function add_file_image(id) {

            var fileList =document.getElementById("file_"+id).files;// js 獲取文件對象

            if (verificationFile(fileList[0])){

                for(var i =0;i

                    var imgSrcI =getObjectURL(fileList[i]);

                        if (id=="other"){

                            imgSrc_other.push(imgSrcI);

                            if(fileList[i].size/1024 >100) { //大於100kb,進行壓縮上傳

                                 fileResizetoFile(fileList[i],0.6,function(res){

                                  imgFile_other.push(res);

                                })

                            }else{

                                imgFile_other.push(res);

                        }

                    }

                    addNewContent(id);

                }

        }

    //新增圖片

    function addNewContent(obj) {

        //刪除原先

        $("#ul_"+obj).html("");

        //判斷循環新增

        var  text="";

        if (obj=="other"){

           for(var a =0;a < imgSrc_examReportCard.length;a++) {

            text +='<li><input type="file" id="file_other" class="file_input" onchange="add_file_image('other')"></li>';

           }

        }else{

            console.log('臟數據');

        }

        var oldBox ="<li><div class=\"filediv\"><span>+</span>\n" +

        "<input type=\"file\" id=\"file_"+obj+"\"  class=\"file_input\" onchange=\"add_file_image('"+obj+"')\">\n" +

        "</div></li>";

        $("#ul_"+obj).html( text+localText);

    }

    使用formData上傳

    var form =document.getElementById("form_addArchive");//表單id

    var formData =new FormData(form);

    $.each(imgFile_other,function(i, file){

        formData.append('imgFileOther', file);

    });

$.ajax({

    url:url,

    type:'POST',

    async:true,

    cache:false,

    contentType:false,

    processData:false,

    dataType:'json',

    data:formData,

    xhrFields:{

        withCredentials:true

    },

    success:function(data) {

     }

  },

  error:function(XMLHttpRequest, textStatus, errorThrown) {

    }

})

後台使用@RequestParam(value ="imgFileOther", required=false) List<MultipartFile> imgFileOther, 接受

//獲取圖片url以便顯示       

//文件格式驗證

//圖片壓縮

熱點內容
編譯編譯有什麼 發布:2025-02-04 08:05:52 瀏覽:732
讓外網訪問內網伺服器 發布:2025-02-04 08:02:20 瀏覽:781
奶塊腳本菜地 發布:2025-02-04 07:46:35 瀏覽:238
條形碼識別源碼 發布:2025-02-04 07:45:55 瀏覽:457
mysql資料庫數據同步 發布:2025-02-04 07:41:07 瀏覽:760
安卓手機下載哪個北斗地圖 發布:2025-02-04 07:35:26 瀏覽:854
查詢伺服器ip地址代碼 發布:2025-02-04 07:08:28 瀏覽:675
python全雙工 發布:2025-02-04 06:57:46 瀏覽:196
c語言動態內存 發布:2025-02-04 06:57:06 瀏覽:78
sql倒序查詢 發布:2025-02-04 06:49:18 瀏覽:196