如何使用招摇快车兆瓦上传文件,并表达什么?
我想用快递框架上传多形式的数据。我使用swagger-node用快递给我的API。现在,我已经写了在招摇YAML文件下面上传文件:
/picture/students:
# binds a127 app logic to a route
x-swagger-router-controller: bus_api
post:
description: Upload a picture
# used as the method name of the controller
operationId: uploadStudentPic
consumes:
- multipart/form-data
parameters:
- in: formData
name: imageFile
type: file
description: The file to upload.
required: true
responses:
"200":
description: OK
schema:
# a pointer to a definition
$ref: "#/definitions/SuccessResponseStr"
但现在我不知道怎么用它来上传图片。有没有什么内在的设施上传图像中招摇?
回答如下:虽然这是在这里的老问题是你如何能做到这一点。我们使用这里的express-fileupload
NPM模块,它使我们的生活更轻松。以下是简单的片段上载文件并将其保存到我们的服务器,昂首阔步YAML保持不变。
//import the module
const fileUpload = require('express-fileupload');
// Add this in express setup
app.use(fileUpload());
// Your controller function, where you will get the file and store it as needed
function uploadStudentPic(req: any, res: any, next: any) {
var startup_image = req.files.imageFile;
var fileName = startup_image.name;
startup_image.mv(__dirname + '/images/' + fileName, function (err: any) {
if (err) {
res.status(500).send(err);
}
res.json({
"message": "File Uploaded"
});
});
}
注意:上面的代码是一个单一的与简单的使用情况,您可能需要做根据您的要求更多一些配置。如果你只是想上传一个文件并将其存储在服务器上这应该工作。
一个重要的事情,这里要注意的是招摇不必知道我们正在使用的模块,或者也不招摇提供了一个内置的工具来上传图片。
我们正在做的声明中我们的API中的问题,更具体地说,这些线...
parameters:
- in: formData
name: imageFile
type: file
description: The file to upload.
required: true
......被指定上述职位API需要一个参数命名imageFile
这应该是一个文件,并通过这个API所需的功能。而如果招摇验证中间件在您的明确和招摇节点配置启用后,我们的API将验证传入的请求,并在情况下,文件没有上传400 Bad Request
错误响应。
如果你想swagger-node
的一个示例配置,swagger-tools
与招摇快递中间件,你可以找到在this answer一些细节发表我(对不起公布:P)
如何使用招摇快车兆瓦上传文件,并表达什么?
我想用快递框架上传多形式的数据。我使用swagger-node用快递给我的API。现在,我已经写了在招摇YAML文件下面上传文件:
/picture/students:
# binds a127 app logic to a route
x-swagger-router-controller: bus_api
post:
description: Upload a picture
# used as the method name of the controller
operationId: uploadStudentPic
consumes:
- multipart/form-data
parameters:
- in: formData
name: imageFile
type: file
description: The file to upload.
required: true
responses:
"200":
description: OK
schema:
# a pointer to a definition
$ref: "#/definitions/SuccessResponseStr"
但现在我不知道怎么用它来上传图片。有没有什么内在的设施上传图像中招摇?
回答如下:虽然这是在这里的老问题是你如何能做到这一点。我们使用这里的express-fileupload
NPM模块,它使我们的生活更轻松。以下是简单的片段上载文件并将其保存到我们的服务器,昂首阔步YAML保持不变。
//import the module
const fileUpload = require('express-fileupload');
// Add this in express setup
app.use(fileUpload());
// Your controller function, where you will get the file and store it as needed
function uploadStudentPic(req: any, res: any, next: any) {
var startup_image = req.files.imageFile;
var fileName = startup_image.name;
startup_image.mv(__dirname + '/images/' + fileName, function (err: any) {
if (err) {
res.status(500).send(err);
}
res.json({
"message": "File Uploaded"
});
});
}
注意:上面的代码是一个单一的与简单的使用情况,您可能需要做根据您的要求更多一些配置。如果你只是想上传一个文件并将其存储在服务器上这应该工作。
一个重要的事情,这里要注意的是招摇不必知道我们正在使用的模块,或者也不招摇提供了一个内置的工具来上传图片。
我们正在做的声明中我们的API中的问题,更具体地说,这些线...
parameters:
- in: formData
name: imageFile
type: file
description: The file to upload.
required: true
......被指定上述职位API需要一个参数命名imageFile
这应该是一个文件,并通过这个API所需的功能。而如果招摇验证中间件在您的明确和招摇节点配置启用后,我们的API将验证传入的请求,并在情况下,文件没有上传400 Bad Request
错误响应。
如果你想swagger-node
的一个示例配置,swagger-tools
与招摇快递中间件,你可以找到在this answer一些细节发表我(对不起公布:P)