Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot upload binary using files.upload facet #103

Closed
pveyes opened this issue Jan 14, 2016 · 7 comments
Closed

Cannot upload binary using files.upload facet #103

pveyes opened this issue Jan 14, 2016 · 7 comments

Comments

@pveyes
Copy link

pveyes commented Jan 14, 2016

Version: 2.0.0-beta2

Cannot send binary file to upload via files.upload. I tried using Buffer and ReadStream (via fs.createReadStream) but no luck.

Here's my code (more or less)

var api = new WebClient(token);
var opts = {
  filename: 'test.jpg',
  file: fs.createReadStream('test.jpg')
};
api.files.upload(opts, function (err, res) {
  console.log(res);
  // { ok: false, error: 'no_file_data' }
});

If I call api directly using request module, I can sent them (using r.form())

var r = request.post('https://slack.com/api/files.upload', function (err, res, body) {
  // this works
});

var form = r.form();
form.append(token, token);
form.append('filename', 'test.jpg');
form.append(file, fs.createReadStream('test.jpg');
@ghost
Copy link

ghost commented Jan 14, 2016

Thanks, will check this out today

ghost pushed a commit that referenced this issue Jan 22, 2016
…ck API correctly, by switching to formData mode with the request module when a file is supplied in the args to an API method.

Fixes #103
@ghost
Copy link

ghost commented Jan 22, 2016

Sorry for the delay on this, I got busy and then was on vacation for a week.

I believe the above PR fixes this.

@ghost ghost closed this as completed Jan 22, 2016
ghost pushed a commit that referenced this issue Feb 13, 2016
…ck API correctly, by switching to formData mode with the request module when a file is supplied in the args to an API method.

Fixes #103
@webus
Copy link

webus commented Jul 12, 2016

It's not working still. I tried to send a picture in chat but Slack sees it as a text and it shows as text.

let WebClient = require("@slack/client").WebClient;
let web = new WebClient(accessToken);
let opts = {
    content: fs.readFileSync(imageFilePath),
    filename: 'image.jpg',
    title: 'image.jpg',
    channels: 'demo'
};
web.files.upload(msg.photo.caption, opts, (err, res) => {
    if (err) {
        // no errors here
    } else {
        // here i receive the data about the uploaded file, and there it is listed as plain text
    }
});

Of course, I found the solution on the advice of @pveyes.

@pveyes
Copy link
Author

pveyes commented Jul 13, 2016

@webus shouldn't you use file instead of content in opts?

@jsarafajr
Copy link

jsarafajr commented Dec 27, 2016

@l12s
Getting Error: no_file_data while uploading Buffer object with files.upload. Seems that this is connected with issues above.

// doesn't work (see trace log below)
const image = fs.readFileSync('image.png');
await bot.files.upload('image.png', {
  file: image
});

// text buffer doesn't work too
await bot.files.upload('text.txt', {
  file: new Buffer('hello world');
});

// uploads image as Plain Text (incorrect)
await bot.files.upload('image.png', {
  content: image
});

// works well with streams
const imageStream = fs.createReadStream('image.png')
await bot.files.upload('image.png', {
  file: imageStream
});
2016-12-27T13:58:29.415Z - error:  Error: no_file_data
     at handleHttpResponse (/bot/node_modules/@slack/client/lib/clients/transports/call-transport.js:105:17)
     at handleTransportResponse (/bot/node_modules/@slack/client/lib/clients/transports/call-transport.js:155:19)
     at apply (/bot/node_modules/lodash/lodash.js:499:17)
     at wrapper (/bot/node_modules/lodash/lodash.js:5356:16)
     at Request.handleRequestTranportRes (/bot/node_modules/@slack/client/lib/clients/transports/request.js:20:5)
     at apply (/bot/node_modules/lodash/lodash.js:499:17)
     at Request.wrapper [as _callback] (/bot/node_modules/lodash/lodash.js:5356:16)
     at Request.self.callback (/bot/node_modules/request/request.js:186:22)
...

@GyozaGuy
Copy link

GyozaGuy commented Jun 7, 2017

I can also verify that no_file_data is returned when using readFileSync, but using createReadStream does work.

@clavin
Copy link
Contributor

clavin commented Jun 7, 2017

See my answer to #307 for uploading a buffer via the files.upload facet.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants