node.js - NodeJS File Creation as an Object? -
i dont want save file locally, want create file object uploaded through api.
var request = require("request"); var fs = require("fs"); var arr = [ [4, 56, 7, 3, 345], [34, 5, 2, 5, 7] ]; var r = requests.post("https://slack.com/api/files.upload", function(err, res,body){ console.log(body)'' } var form = r.form(); form.append('token', token) form.append("filename", filename) form.append('file', new buffer(arr)); i want know if possible not save file locally , add in file data form submitted through api?
yes, can pipe file directly request without saving value. however, depends on data coming , how can access it. when fs.createreadstream('<filepath>') you're creating readstream pass request part of form data. request uses stream read file , pass through resource requested.
the below code, take file accessible server , pass through request desired endpoint.
var request = require('request'); var fs = require('fs'); var formdata = { token: token, filename: filename, file: fs.createreadstream('<filepath>'); }; request({ method: 'post', url: 'https://slack.com/api/files.upload', form: formdata }, function(err, res, body) { console.log(json.stringify(body, null, 2)); });
Comments
Post a Comment