0

in my Electron app I need to upload a file (.mp3) using a normal html input and then save it on the disk.

I'm reading the file using the browser's FileReader:

  const reader = new FileReader();
  reader.onload = () => {
    resolver.next(reader.result as string);
    resolver.complete();
  };
  reader.readAsBinaryString(file);

Then I sent the readed content like this:

this.electronService.ipcRenderer.on('aaaSuccess', (_, newPath) =>
            this.store$.dispatch(HomeActions.changeSuccess({ soundName: action.sound.name, newPath })));

          this.electronService.ipcRenderer.send('aaa', { fileName: file.name, content: base64 });

Then I pass the readed binary string to the mainProcess like this:

ipcMain.on('aaa', (event, { fileName, content }) => {
    var newPath = path.join(app.getPath('userData'), fileName);
    fs.writeFile(newPath, content, function (err) {
        if (err) { return console.log('error is writing new file', err) }
        event.reply('aaaSuccess', newPath)
    });
})

This code works, but the dimension in bytes of the saved file is different from the original one, and it can't be opened using an mp3 player

Thanks a lot

user3471528
  • 3,013
  • 6
  • 36
  • 60
  • If the file comes from the local filesystem, you can just send the file path to the main process and copy the file there, no need to roundtrip the MP3 data through JSON. How are you sending the content to main, and what do you get as `content` in the event handler? You're probably hitting some issue with trying to marshal a Blob from browser to Node. – millimoose Apr 06 '20 at 12:37
  • Thank you @millimoose. Unfortunately I need to roundtrip it – user3471528 Apr 06 '20 at 12:40
  • Okay, so what exactly is `content` in the main process handler? Set a breakpoint if at all possible so you can look at the type etc., this is probably faster for you than for me to start an Electron scratchpad app. (Don't want to use my main project because then I'll just forget the code in it :D) – millimoose Apr 06 '20 at 12:43
  • :D Using reader.readAsBinaryString I get this string for example (it is a 7kb audio): https://pastebin.com/m9kNM1u2 – user3471528 Apr 06 '20 at 14:40
  • it works in this way: https://stackoverflow.com/questions/43487543/writing-binary-data-using-node-js-fs-writefile-to-create-an-image-file – user3471528 Apr 06 '20 at 14:47
  • 1
    Oh `readAsBinaryString` literally gives you a string? In that case I suspect an encoding error - I don't even know what a "binary string" is supposed to be in a Unicode environment, there's no way to represent bytes over 128 in Unicode. Try going through either `readAsDataUrl()` that you'll decode from base64 in main, or `readAsArrayBuffer()` and pray Electron IPC supports binary arrays, or maybe pass an explicit encoding to `writeFile()`. Another avenue would be using a very short test file with like one byte over 128, and looking at what comes out on the other side of IPC. – millimoose Apr 06 '20 at 15:14
  • I'll play around with this later when I get to my macbook, create-electron-app is broken on Windows and I cba to wire things together manually. – millimoose Apr 06 '20 at 15:14

0 Answers0