天天看點

formData SSM 架構實作多圖上傳

formData部分代碼:

var formData = new FormData();
for(var ii = 0; ii < flag; ii++){
    if (editor[ii+1] != undefined){
        //文本
        alert(ii+1);
        var single_json = {
            "inner": editor[ii+1].txt.html(),
            "order": ii+1
        };
        result.push(single_json);
     }else {
        //圖檔
        //擷取id
        var pic_id = "#pic"+(ii+1).toString();
        formData.append('file', $(pic_id)[0].files[0]);

        //FormData.set 和 append() 的差別在于,如果指定的鍵已經存在, FormData.set 會使用新值覆寫已有的值,而 append() 會把新值添加到已有值集合的後面。
    //注意:如果你的伺服器端是PHP那麼為了與php命名習慣一緻在名稱中需要添加[],如formData.append('files[]',files[i]);


        //這裡加一個order的數組
        var single_order = {
             "order": ii+1
        };
        files_order.push(single_order);

      }
}
           

//FormData.set 和 append() 的差別在于,如果指定的鍵已經存在, FormData.set 會使用新值覆寫已有的值,而 append() 會把新值添加到已有值集合的後面。

//注意:如果你的伺服器端是PHP那麼為了與php命名習慣一緻在名稱中需要添加[],如formData.append('files[]',files[i]);

服務端代碼

Controller:

/**
     * 上傳feeds流的段子
     * @param sentence
     * @param response
     * @return
     */
    @RequestMapping(value = "upload_feeds_sentences.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse upload_feeds_sentences(String files_order,@RequestParam(value = "file",required = false) MultipartFile[] files,@RequestParam(value = "pic",required = false) MultipartFile pic,@RequestParam(value = "video_file",required = false) MultipartFile video_file,String title,String select,String kind,String author,String sentence, HttpServletResponse response, HttpServletRequest request ){
        return iAdminService.upload_feeds_sentences(files_order,files,pic,video_file, title, select, kind, author,sentence, response,request);
    }
           

service:

formData SSM 架構實作多圖上傳

繼續閱讀