File Download using fs.createWriteStream makes bloated and corrupt files


#1

I’m trying to download files in an Electron app using Dialog and fs, but I’m getting files that are corrupted and nearly twice the size they should be.

dialog.showSaveDialog({ title: 'Downloader', defaultPath: '/' + fileName },
            function (filePath) {
                var fileStream = fs.createWriteStream(filePath);
                fileStream.on('open', function(fd) {
                    $http.get(url + fileName)
                        .success(function(data, status, headers, config){
                            fileStream.write(data);
                            fileStream.end();
                        })
                        .error(function(data, status, headers, config){
                            console.log('product download failed');
                        });
                });
            });

I attempted downloading a basic .txt file with using this code and the file came through fine - the right size and uncorrupted. But anything larger gives me a bloated file size and a corrupted file.


#2

I suspect you’re running into an encoding issue. My guess is that:

  1. You’re downloading a binary file (JPG, MP4, MP3, whatever)
  2. The file is corrupt because the default encoding for fs.createWriteStream is UTF-8 … within which some byte values or byte patterns are invalid
  3. You don’t see this problem when downloading a text file because it is stored in UTF-8 (or it is “7-bit clean” so it doesn’t matter what encoding you’re using)

Solution if the above is correct: change the defaultEncoding to binary.


#3

leedohm - Thank You! The binary encoding fixed the file size issue. The files still come through corrupted though(I’m downloading primarily .zip and .rar files). Any thoughts?


#4

Perhaps it is being downloaded encoded somehow too? Have you checked the documentation on your HTTP library?