Filesaver.js: corrupt data/file (*.xlsx)

Created on 7 Sep 2016  路  9Comments  路  Source: eligrey/FileSaver.js

I'm trying to save an xslx file, which works on chrome but fails on FF (corrupt data)

Here's what i use:

var b64toBlob = function(b64Data, contentType, sliceSize) {
    contentType = contentType || '';
    sliceSize = sliceSize || 512;
    var byteCharacters = atob(b64Data);
    var byteArrays = [];
    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);
        var byteNumbers = new Array(slice.length);
        for (var i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }
        var byteArray = new Uint8Array(byteNumbers);
        byteArrays.push(byteArray);
    }
    var blob = new Blob(byteArrays, {type: contentType});
    return blob;
}
var excel = EB.createFile(workbook).then(function(data) {
    var blob = b64toBlob(data, 'application/octet-stream');
    saveAs(blob, 'Film-Sammler_' + moment().format('DD.MM.YYYY__HH.mm.ss') + '.xlsx');
});

it seems the blob gets corrupted while on FF, maybe a size Problem (i have about 350rows here, ~950kb data)

anybody an idea?

Make a Blob firefox help wanted

Most helpful comment

All 9 comments

1MB is nothing... considering that most computers now days have 2GB of ram.
Is there anyway you could perhaps avoid the base64 and get a ArrayBuffer or Blob instead?

Not so familiar with excel-builder, but there looks like it is a option for using base64 or not
https://github.com/stephenliberty/excel-builder.js/blob/303cced696d87ccbc520eb6d8f3e296644cbcd20/src/excel-builder.js#L28

Oh, I think i know why. The atob is fucked up!
https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem

But there is a simpler solution to turn a base64 to a blob: http://stackoverflow.com/a/36183085/1008999

Bingo!

Only Problem now is on FF openWith thinks it's a zip file. :-)

This ain't really a problem with FileSaver. It's how you create the blob, so I'm closing this

Actually what i'm finally doing is :

var excel = EB.createFile(workbook, {type: 'blob'});
excel.then(function (blob){
   var data = new Blob([blob], {type:'base64'});
   saveAs(data, filename);
});

now it works like intended: files open in excel directly, even in FF :-)

Eh? When you get the file it's no longer base64 so setting the type to base64 is wrong, but it don't mather so much what type it's. should be more like

var data = new Blob([blob], {type: blob.type});

But you don't even need that...
You can just do

excel.then(function (blob){
   saveAs(blob, filename);
});

Actually the Blob Excelbuilder.js is generating has blob.type: 'application/zip', which seems wrong.
So i'm correcting this with:
var data = new Blob([blob], {type: 'anything goes, really anything'});
And it works.

File an issue to excel-builder and say that it's wrong... Not that it mather so much but excel is actually built upon a zip structor [1]

If you would look at it like a program on a mac it would be one file but really it's more like a folder where you can show package content of all the files.

Only different is the extension in which it opens the file in excel instead of unarchiver and render it like a table

Was this page helpful?
0 / 5 - 0 ratings