天天看點

plupload 結合springmvc上傳完成後界面不跳轉問題

利用plupload上傳完成後,在mvc背景return值界面一點反應也沒有,但是Eclipse控制台會顯示已經跳轉完成,搞了好長時間沒搞定,最後不得換跳轉方式,利用plupload帶有的幾個時間解決了。

關于plupload: 檢視http://www.cnblogs.com/2050/p/3913184.html     講解的非常詳細。還有很多例子。

記錄下我遇到的問題,利用bootstrap彈出的modal進行上傳,上傳完成後,怎樣都跳轉不了,除非另外點選一個動作,但是這樣的話會影響我背景的批量添加工作。

plupload 結合springmvc上傳完成後界面不跳轉問題

我的plupload配置,這裡我用的是velocity模闆,跟jsp差別不大:

var uploader = new plupload.Uploader({ //執行個體化一個plupload上傳對象
    browse_button : 'browse',//選擇檔案按鈕的id
    url : '$!request.getContextPath()/receive/customer/batchImpInit.io',
    flash_swf_url : '$!request.getContextPath()/static/js/plupload/Moxie.swf',//flash動作路徑
    silverlight_xap_url : '$!request.getContextPath()/static/js/plupload/Moxie.xap',
    filters: {
      mime_types : [ //隻允許上傳圖檔檔案和rar壓縮檔案
        { title : "excel", extensions:"xls,xlsx"}, 
      ],
      max_file_size : '2048kb', //最大隻能上傳100kb的檔案
      prevent_duplicates : true //不允許隊列中存在重複檔案
    },
    rename:true,
    multipart:true,
    multi_selection:false//隻能選取一個
});

uploader.init(); //初始化

//綁定檔案添加進隊列事件
uploader.bind('FilesAdded',function(uploader,files){
    for(var i = 0, len = files.length; i<len; i++){
        var file_name = files[i].name; //檔案名
        //構造html來更新UI
        var html = '<li id="file-' + files[i].id +'"><p class="file-name">' + file_name + '</p> <b id="removeFile" data-val=""+files[i].id+"">删除</b> <p class="progress"></p></li>';
        $(html).appendTo('#file-list');
    }
});

//删除已選擇的文檔  這個是删除一個已選擇的檔案
$(document).on("click","#removeFile",function(){
    $(this).parent('li').remove();
    //uploader.removeFile($(this).attr("data-val"));
    var toremove = "";
    var id=$(this).attr("data-val");
    for(var i in uploader.files){
        if(uploader.files[i].id === id){
            toremove = i;
        };
    }
    uploader.files.splice(toremove, 1);        
    console.log("XXX"+$(this).attr("data-val"));
});

//綁定檔案上傳進度事件
uploader.bind('UploadProgress',function(uploader,file){
    $('#file-'+file.id+' .progress').css('width',file.percent + '%');//控制進度條
});
uploader.bind('FileUploaded',function(uploader,file,responseObject){
    $("#container").html('<form action="$!request.getContextPath()/batchImpRC.io" method="post" id="form2">'+
            '<input type="text" name="filepath" value='+responseObject.response+' />'
            +'</form>');
    $("#form2").submit();
});

//上傳按鈕
$('#upload-btn').click(function(){
    uploader.start(); //開始上傳
});                

問題一直解決不了後,隻能轉換方式,我的想法很簡單,先将上傳來的檔案進行儲存,然後通過plupload的

cnblogs 無雙大神中關于這個事件的解釋

FileUploaded

當隊列中的某一個檔案上傳完成後觸發

監聽函數參數:(uploader,file,responseObject)

uploader

為目前的plupload執行個體對象,

file

為觸發此事件的檔案對象,

responseObject

為伺服器傳回的資訊對象,它有以下3個屬性:

response:伺服器傳回的文本

responseHeaders:伺服器傳回的頭資訊

status:伺服器傳回的http狀态碼,比如200

背景儲存檔案操作如下:

@RequestMapping(value = "/receive/customer/batchImpInit.io")
    public String batchImpInit(HttpServletRequest request,String name,
            HttpServletResponse response ,@RequestBody MultipartFile file)
            throws Exception {
        String realPath = request.getServletContext().getRealPath("");
        String path = realPath+CUSTOMER_RECEIVE_DIR+Utils.generateUUID()+uploadFileName;
        File realFile = new File(path);
        file.transferTo(realFile);//将muitipartFile轉為普通的file
        
        PrintWriter writer = response.getWriter();
        writer.write(path);//向前台響應檔案的存儲路徑
        return null;
    }                

這樣的話就能通過背景傳回已儲存檔案的絕對路徑,然後通過reponse傳回到這個事件中再進行一次跳轉即可進行檔案的批量添加操作,具體步驟如下:

在頁面新添加一個 form并直接送出
$("#container").html('<form action="$!request.getContextPath()/batchImpRC.io" method="post" id="form2">'+
            '<input type="text" name="filepath" value='+responseObject.response+' />'
            +'</form>');
    $("#form2").submit();

背景操作如下
@RequestMapping(value = "/batchImpRC.io", method = RequestMethod.POST)
    public String batchImpRC(HttpServletRequest request,HttpServletResponse response,
    String filepath) throws IOException {}                

這樣就可以在背景完成我的資料操作了,而且也不必使用者再多進行一次點選操作

效果:

plupload 結合springmvc上傳完成後界面不跳轉問題

版權聲明:本文為CSDN部落客「weixin_33924220」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_33924220/article/details/91678284