免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
javascript – 在瀏覽器的本地存儲中下載并保存文件

我有一個提供文件的Java Web應用程序:

@RequestMapping(value = "/pdf/download", method = RequestMethod.GET)public void download(        HttpServletRequest request,         HttpServletResponse response,         @RequestParam(value = "id", required = true) Long id) throws IOException {    File pdfFile = pdfFileManager.getFromId(id);    response.setContentType("application/pdf");    response.addHeader("Content-Disposition", "attachment; filename=download");    response.setContentLength((int) pdfFile.length());    FileInputStream fileInputStream = null;    OutputStream responseOutputStream = null;    try {           fileInputStream = new FileInputStream(pdfFile);        responseOutputStream = response.getOutputStream();        int bytes;        while ((bytes = fileInputStream.read()) != -1) {            responseOutputStream.write(bytes);        }        responseOutputStream.flush();    } finally {        fileInputStream.close();        responseOutputStream.close();    }}

我在客戶端檢索文件,base64使用FileReader對其進行編碼:

$.ajax({    url: "/pdf/download?id="   id,    dataType: "application/pdf",    processData: false}).always(function(response) {    if(response.status && response.status === 200) {       savePdf(response.responseText, "download_"   id);    } }); function savePdf(pdf, key) {    var blob = new Blob([pdf], {type: "application/pdf"});    var fileReader = new FileReader();    fileReader.onload = function (evt) {        var result = evt.target.result;        try {            localStorage.setItem(key, result);        } catch (e) {            console.log("Storage failed: "   e);        }    };    fileReader.readAsDataURL(blob);}

問題是本地存儲中保存的值不正確.編碼數(shù)據(jù)與我使用this snip上傳PDF時得到的數(shù)據(jù)不同.我不知道問題是我如何在客戶端中提供文件或編碼過程.

存儲的值是這樣的

data:application/pdf;base64,JVBERi0xLjQKJe /ve /ve /ve /vQoxIDAgb...

代替

data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Ue...

解決方法:

解決了將請求的響應類型設置為blob的問題:

var xhr = new XMLHttpRequest(); xhr.open("GET", "/pdf/download?id="   id); xhr.responseType = "blob";xhr.onload = function() {    if(xhr.status && xhr.status === 200) {        savePdf(xhr.response, "download_"   id);    } }xhr.send();function savePdf(pdf, key) {    var fileReader = new FileReader();    fileReader.onload = function (evt) {        var result = evt.target.result;        try {            localStorage.setItem(key, result);        } catch (e) {            console.log("Storage failed: "   e);        }    };    fileReader.readAsDataURL(pdf);}
來源:https://www.icode9.com/content-1-275951.html
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
如何在網(wǎng)頁中如何下載一個JPG(或pdf)文件,而不在瀏覽器中打開?
在本地存儲localStorage中保存圖片和文件(任意格式)
深入研究HTML5實現(xiàn)圖片壓縮上傳功能
前端文件上傳基礎
移動前端
HTML5中File對象初探
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服