2

There is an <input type="file" id="upload" accept="application/pdf"/> that accepts the pdf that should be saved to a location within the electron app. Saving a file with fs is working just fine but I can't figure out how it works with a pdf. I don't know what I need to hand over to writeFileSync() for data and encoding.

function upload(path){
    "use strict";
    var fs = require('fs');
    var file = $("#upload").prop("files")[0];
    try {
        fs.writeFileSync(path, foo, bar);
    } catch(e) {
        console.log(e);
    }
}
leonheess
  • 16,068
  • 14
  • 77
  • 112
  • Take a look at the bottom answer here: https://stackoverflow.com/questions/31040014/how-to-save-pdf-in-proper-encoding-via-nodejs Does that do it? – dmgig May 08 '18 at 20:54
  • Yeah I found this, too, but it just says `body` for _data_ but it isn't defined.. – leonheess May 08 '18 at 20:56

1 Answers1

4

According to what I've just read, you need to take the file path which you are capturing, and read it with fs.readFile(path[, options], callback)

This returns a 'data' value, which you should then be able to use to write to where you want.

fs.readFile('/tmp/my.pdf', (err, data) => {
  if (err) throw err;
  fs.writeFileSync(path, data, 'binary');
});

https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback

I got started here, have a look: How do I handle local file uploads in electron?

The answer shows a slightly different way of capturing the file name (dialog.showOpenDialog) but I think what you're doing should be fine so long as you are getting the complete path.

dmgig
  • 4,400
  • 5
  • 36
  • 47