Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

java - Handle Byte Array in Vue

I'm new in Vue and now trying to handle an bytearray as an .xls file

For now i have an issue that the file looks like broken

If i'm saving it as file at server side, data looks fine and nothing going wrong

Server side:

    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("Payments");
    for (int i = 0; i < payments.size(); i++) {
        Payment payment = payments.get(i);
        Row row = sheet.createRow(i);

        ...(adding a lot of data)
    }

    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        wb.write(bos);
        wb.close();
        return bos.toByteArray();
    }

Vue side:

axios.post(myRequestAndData)
    .then(resp => {
                var fileURL = window.URL.createObjectURL(new Blob([resp.data]));
                var fileLink = document.createElement('a');
                fileLink.href = fileURL;
                fileLink.setAttribute('download', 'file.xls');
                document.body.appendChild(fileLink);
                fileLink.click();

So how can i handle my ByteArray data to create a file in the right way? Thanks anyway


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved by adding ,{responseType:"blob"} at axios.post/get options

Thanks @JoshuaAngnoe for help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...