Node.js 如何使用restify上传文件
我正试图使用Restify模块将图像文件上传到Rest服务器(更具体地说是Confluence),但出现断言错误。我不确定是否使用了正确的方法将文件上载到REST服务器。谁能给我指一下正确的方向吗 这是我的尝试-Node.js 如何使用restify上传文件,node.js,restify,Node.js,Restify,我正试图使用Restify模块将图像文件上传到Rest服务器(更具体地说是Confluence),但出现断言错误。我不确定是否使用了正确的方法将文件上载到REST服务器。谁能给我指一下正确的方向吗 这是我的尝试- var restify = require('restify'); var header = {'Authorization': 'Basic xxxxx', 'content-type': 'multipart/form-data'}; var client = restify.cr
var restify = require('restify');
var header = {'Authorization': 'Basic xxxxx', 'content-type': 'multipart/form-data'};
var client = restify.createJsonClient({
url: 'http://www.testsite.com',
version: '*',
headers: header
});
var image = "c:\\Users\\abc\\Documents\\bbb.jpg";
var fileStream = fs.createReadStream(image);
var stat = fs.statSync(image);
var size = stat["size"];
var param = "?pageId=123&filename=mynewimage&mimeType=image%2Fjpeg&size=" + size;
fileStream.pipe(
client.post("/plugins/drag-and-drop/upload.action"+param, function(err, req, res, obj) {
if (err) {
return err;
}
})
);
更新:
这是一个断言错误,我得到assert.js:86
throw new assert.AssertionError({
^
AssertionError: body
at Object.module.exports.(anonymous function) [as ok] (c:\Users\abc\myproj\node_modules\restify\node_modules\assert-pl
us\assert.js:242:35)
at JsonClient.write (c:\Users\abc\myproj\node_modules\restify\lib\clients\json_client.js:31:12)
at ReadStream.ondata (_stream_readable.js:540:20)
at ReadStream.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at ReadStream.Readable.push (_stream_readable.js:126:10)
at onread (fs.js:1683:12)
at FSReqWrap.wrapper [as oncomplete] (fs.js:529:17)
抛出新的assert.AssertionError({
^
断言者:主体
在Object.module.exports.(匿名函数)[as ok](c:\Users\abc\myproj\node\u modules\restify\node\u modules\assert pl
us\assert.js:242:35)
在JsonClient.write(c:\Users\abc\myproj\node\u modules\restify\lib\clients\json\u client.js:31:12)
在ReadStream.ondata(_stream_readable.js:540:20)
在ReadStream.emit(events.js:107:17)
在readableAddChunk(_stream_readable.js:163:16)
在ReadStream.Readable.push(_stream_Readable.js:126:10)
在读时(fs.js:1683:12)
在FSReqWrap.wrapper[as oncomplete](fs.js:529:17)
要发送
多部分/表单数据
,您必须使用restify
以外的库,因为它不支持该内容类型
您可以使用内置的支持
multipart/form data
http请求的工具。我正在努力发送一个包含multipart/form data
的请求,并使用restify 5
在API上处理该请求。下面的回答我给出了以下处理这两种情况的代码:
"use strict"
const restify = require('restify'),
plugins = require('restify-plugins'),
fs = require('fs'),
request = require('request'),
FormData = require('form-data');
var server = restify.createServer();
server.use(plugins.multipartBodyParser()); // Enabling multipart
// Adding route
server.post('/upload', (req, res, next) =>{
/**
* Processing request containing multipart/form-data
*/
var uploaded_file = req.files.mySubmittedFile; // File in form
//Reading and sending file
fs.readFile(uploaded_file.path, {encoding: 'utf-8'}, (err, data)=>{
// Returning a JSON containing the file's name and its content
res.send({
filename: uploaded_file.name,
content: data
});
next()
});
});
// Launching server at http://localhost:8080
server.listen(8080, start);
// Client request
function start(){
// Making a request to our API using form-data library
// Post file using multipart/form-data
var formData = {
mySubmittedFile: fs.createReadStream('/tmp/hello.txt')
}
request.post({'url': 'http://localhost:8080/upload', formData: formData}, function(err, res, body){
console.log("Response's body is: "+ body);
});
}
使用:
- 形式-data@2.2.0
- request@2.81.0
- restify@5.0.1
- 恢复原状-plugins@1.6.0
- 节点@6.11.1
我试图不使用库
表单数据
和请求
来只使用restify/http,但我最终得到了一个可怕的长代码。您可能会看到实际的断言错误。createJsonClient()
用于发送JSON数据,而不是二进制(图像)data.Restify没有发送多部分/表单数据
请求的方法,只有原始的应用程序/x-www-form-urlencoded
和应用程序/json
。您的目标服务器需要什么内容类型
?对于该路由,它需要多部分/表单数据。为此,您建议使用什么模块y代码只是将数据上传到远程服务器,我根本不需要实现用户界面。我更喜欢重量轻的东西。感谢您的时间!从restify
4开始,这不再是真的。require('restify-plugins')
不推荐使用require('restify')。插件