ファイルのダウンロードのためのボタンをクリックします

まず、背景を使用します

私たちは、ファイルをアップロードする際に一般的に言えば、ファイルは、ダウンロードの入り口に提供されます。そして、実際には、ファイルがファイルをダウンロード取得することです、ファイルの内容は、プロセスが復帰HTTPレスポンスに書き込まれます。

第二に、フロントエンドの実装

  • フォーム送信フォーム構造

1、導入JSに関連します

<script src="${basepath}/jquery-3.3.1/jquery-3.3.1.js" type="text/javascript"></script>
<script src="${basepath}/downLoadFile.js" type="text/javascript" ></script>

jqueryの-3.3.1のダウンロードリンクアドレス

downLoadFile.jsダウンロードリンクアドレス

2、ダウンロードボタンをクリックしてイベントを拘束

<button οnclick=downLoadFile('"+fileId+"') class='btn btn-info btn-sm pull-right'>下 载</button>

3、downLoadFile.jsソース

function downLoadFile(id){
	var form=$("<form>");    // 定义一个form表单
	form.attr("style","display:none");
	form.attr("target","_blank");
	form.attr("method","post");
	form.attr("action","/download");    // 此处填写文件下载提交路径
	var input1=$("<input>");
	input1.attr("type","hidden");
	input1.attr("name","id");    // 后台接收参数名
	input1.attr("value",id);
	$("body").append(form);    // 将表单放置在web中
	form.append(input1);
	form.submit();    // 表单提交
}

第三に、バックグラウンドのJava実装セクションのコード

/**
  * 附件下载
  */
@RequestMapping(value = "download")
public String downloadFile( HttpServletRequest request, Model model,HttpServletResponse response, RedirectAttributes redirectAttributes,String id) {
    shareFile = fileService.get(id);
    if(shareFile != null){
        String fileName = shareFile.getFilename().replace(" ", "");
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");
        String agent = request.getHeader("USER-AGENT");    // 获取浏览器类型
        String downLoadName = null;  
        try {
            // Edge
            if (null != agent && -1 != agent.indexOf("Edge")) {    
                downLoadName = java.net.URLEncoder.encode(fileName, "UTF-8");
            // Firefox
            } else if (null != agent && -1 != agent.indexOf("Firefox")) {        
                downLoadName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
            // Chrome或360  
            } else if (null != agent && -1 != agent.indexOf("Chrome")) {        
                downLoadName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");     
            } else {  
                downLoadName = java.net.URLEncoder.encode(fileName, "UTF-8");   
            }   
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        response.setHeader("Content-Disposition", "attachment; filename="+downLoadName);
		
        InputStream in = fileService.downloadFile(shareFile);    // 此处是获取文件输入流 
        OutputStream out;
        try {
            out = response.getOutputStream();
            //写文件  
            int b;  
            while((b = in.read()) != -1) {  
                out.write(b);  
            }
            in.close();  
            out.close();  
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

 

公開された47元の記事 ウォン称賛16 ビュー70000 +

おすすめ

転載: blog.csdn.net/zorro_jin/article/details/82905268