简体   繁体   English

的NodeJS。 没有表格上传文件

[英]nodejs. Upload File without a form

I have an express based server side script to upload images and it works fine if I use a html form for uploading. 我有一个基于Express的服务器端脚本来上传图像,如果我使用html表单进行上传,则可以正常工作。 Now, I don't want to use this form and I want to upload files that are stored in filesystem via fs library. 现在,我不想使用此表单,而是要通过fs库上传存储在文件系统中的文件。 I want to do it with another different nodejs script. 我想用另一个不同的nodejs脚本来做。

Is this possible? 这可能吗?

I'll suggest to check request module or specifically this part of the documentation https://github.com/mikeal/request#forms 我建议检查请求模块,或者特别是文档的这一部分https://github.com/mikeal/request#forms

Here is an example: 这是一个例子:

var r = request.post('http://service.com/upload', function optionalCallback (err, httpResponse, body) {
  if (err) {
    return console.error('upload failed:', err);
  }
  console.log('Upload successful!  Server responded with:', body);
})
var form = r.form()
form.append('my_field', 'my_value')
form.append('my_buffer', new Buffer([1, 2, 3]))
form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png')))
form.append('remote_file', request('http://google.com/doodle.png'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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