简体   繁体   中英

How to send file to apps script using plain POST request?

im doing simple file uploader to apps script but I faced some troubles with uploading data as file. Lets say I have this code:

function doPost(e) {
  console.log(e)
}

and I do a simple POST request in node.js

  let formData = {
    theFile: {
      value: fs.createReadStream('myawersome.file'),
      options: {
        filename: 'myawersome.file',
        contentType: 'some/mimetype'
      }
    }
  }
  let params = {
    url: 'my-script-url',
    followAllRedirects: true,
    formData: formData
  }
  request.post(params)

So, whats the problem. I dont see any files in my e param in doPost . That is my console.log output

{"queryString":"","parameter":{},"contextPath":"","parameters":{},"contentLength":9483}

I can see that I have some data in request, but everything is empty. e.parameters.theFile and e.theFile are undefined . Where is my file?

the call of createReadStream just create the stream but don't read the file

to read the file, try that :

var rs = fs.createReadStream('myawersome.file');


rs.on("data", function (chunk) {

    var content = chunk.toString();


    var formData = {
        theFile: {
            value: content,
            options: {
                filename: 'myawersome.file',
                contentType: 'some/mimetype'
            }
        }
    }

    var params = {
        url: 'my-script-url',
        followAllRedirects: true,
        formData: formData
    }

    request.post(params)

});
rs.resume(); // this launches the read

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM