Search code examples
javascriptphotoshop

Send a file to an API from Photoshop


I need to take the current active document open in photoshop and send it to a JS function who need the file in parameter.

I tried by getting the path of the file like this :

csInterface.evalScript('app.documents[0].fullName.parent.fsName.toString()',function(result){
    csInterface.evalScript('app.documents[0].name', function(res) {
      var response = result+"/"+res;
      var path = response.replace(/\\/g, '/');
      console.log(path);
      var file = window.cep.fs.readFile(path);
      console.log(file);
      //projectCreateDownloadToken(file.data);
    });

and then use window.cep.fs.readFile to get the file from the path but it only take the data and not the file.

Maybe I need to use

 var arg = 'file=@'+path;
  var url ="your_server_url ";
  console.log(window.cep.process.createProcess('/usr/bin/curl','--form',arg,url));

But I'm looking for an other solution. Thank you for any helps !

Result I have atm


Solution

  • I was able to do it using this code

    csInterface.evalScript('app.documents[0].fullName.fsName', function(res) {
          let fileInBase64 = window.cep.fs.readFile(res, cep.encoding.Base64);
          let fileName =  res.split('\\').pop().split('/').pop();
          urltoFile('data:image/png;base64,'+fileInBase64.data, fileName)
          .then(function(file){
            //do what you want with the file
          });
        });
      }, false);