javascript - Download and save file in browser's local storage - Stack Overflow

I have a Java web app that serves a file:@RequestMapping(value = "pdfdownload", method = Re

I have a Java web app that serves a file:

@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();
    }
}

I retrieve the file in the client and base64 encode it using 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);
}

The problem is that the value saved in the local storage is not correct. The encoded data differs from the one i get when i upload the PDF using this snip. I don't know if the problem is how i serve the file or the encoding process in the client.

The value stored is something like this

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

instead of

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

I have a Java web app that serves a file:

@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();
    }
}

I retrieve the file in the client and base64 encode it using 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);
}

The problem is that the value saved in the local storage is not correct. The encoded data differs from the one i get when i upload the PDF using this snip. I don't know if the problem is how i serve the file or the encoding process in the client.

The value stored is something like this

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

instead of

data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Ue...
Share Improve this question edited Nov 17, 2015 at 22:28 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Nov 17, 2015 at 14:57 jorgeprjorgepr 3074 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Solved the problem setting the request's response type to 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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745577808a4634073.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信