Search code examples
node.jsfilepicker

Can't get URL of a file stored on filepicker, using node.js


Trying to find a solution for storing some files from a lil' app I'm currently building, I tried to use a filepicker node module and tried the simplest example I could create:

app.get('/test', function(req, res) {
  fs.readFile('myFile.txt', function (err, buf){
      filepicker.getUrlFromBuffer(buf, {persist: true}, function (err, url) {
          console.log("myfile.txt is now stored at " + url);
      });
      return res.send('something');
  });

I get this error message : "TypeError: Cannot read property 'url' of undefined". But when I check the console on filepicker.io site, I can see that the file has been uploaded.

Here is the github repo I got the filepicker node module from: https://github.com/treygriffith/filepicker/blob/master/README.md

Thank you guys!


Solution

  • Most likely filepicker.io changed their API since that module was last updated (2012). Here is documentation for their current REST API which should be easy enough to use manually with a module like request to upload the file. Example:

    var request = require('request');
    var FILEPICKER_API_KEY = 'foobarbaz';
    
    var formData = {
          fileUpload: fs.createReadStream(__dirname + '/unicycle.jpg')
        },
        cfg = {
          url: 'https://www.filepicker.io/api/store/S3?key=' + FILEPICKER_API_KEY,
          formData: formData
        };
    request.post(cfg, function(err, res, body) {
      if (err)
        return console.error('upload failed:', err);
      var response;
      try {
        response = JSON.parse(body);
      } catch (ex) {
        console.log(ex);
      }
      if (response)
        console.dir(response);
    });