我正在使用 Multer 在 swagger UI 中制作上传文件系统。
我发现了这个屏幕截图问题 - 上传不是一个功能
这是类别。 uploads.js
const multer = require("multer");
const Path = require("path");
const storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, "./uploads/categories");
},
filename: function (req, file, callback) {
callback(null, Date.now() + "-" + file.originalname);
}
});
const fileFilter = (req, file, callback) => {
const acceptableExt = [".png", ".jpg", ".jpeg"];
if (!acceptableExt.includes(Path.extname(file.originalname))) {
return callback(new Error("Only .png, .jpg and .jpeg format allowed!"));
}
const fileSize = parseInt(req.headers["content-length"]);
if (fileSize > 1048576) {
return callback(new Error("File Size Big"));
}
callback(null, true);
};
let upload = multer({
storage: storage,
fileFilter: fileFilter,
fileSize: 1048576,
});
module.exports = upload.single("categoryImage");
抱歉,格式化程序无法工作......
这是我的Category.service.js
const { category } = require("../models/category.model");
async function createCategory(params, callback) {
if (!params.categoryName) {
return callback(
{
message: "Category Name Required",
},
""
);
}
const model = new category(params);
model.save().then((response) => {
return callback(null, response);
}).catch((error) => {
return callback(error);
});
};
这是我的类别。控制器.js
const categoriesService = require("../services/category.service");
const upload = ("../middleware/category.upload");
exports.create = (req, res, next) => {
upload(req, res, function (err) {
if (err) {
next(err);
}
else {
const path = req.file != undefined ? req.file.path.replace(/\\/g, "/"
) : "";
var model = {
categoryName: req.body.categoryName,
categoryDescription: res.body.categoryDescription,
categoryImage: path != "" ? "/" + path : "",
};
categoriesService.createCategory(model, (error, results) => {
if (error) {
return next(error);
}
else {
return res.status(200).send({
message: "Success",
data: results,
});
}
});
}
});
};
这是我的 swagger.json
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Grocery App",
"description": "Grocery App - Flutter/NodeJs",
"license": {
"name": "MIT",
"url": "https://opensource.org/licenses/MIT"
}
},
"host": "localhost:8000",
"basePath": "/",
"tags": [
{
"name": "Category",
"description": "Category API"
}
],
"schemes": ["http"],
"consumes": ["application/json"],
"produces": ["application/json"],
"paths": {
"/api/category": {
"get": {
"tags": ["Category"],
"summary": "Get all Categories",
"parameters": [
{
"name": "categoryName",
"in": "query",
"required": false,
"description": "Category Name",
"type": "string"
},
{
"name": "page",
"in": "query",
"required": false,
"description": "Page Number",
"type": "integer",
"default": 1
},
{
"name": "pageSize",
"in": "query",
"required": false,
"description": "Page Size",
"type": "integer",
"default": 10
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ServiceResponse"
}
}
}
},
"post": {
"tags": ["Category"],
"summary": "Create Category API",
"parameters": [
{
"name": "categoryName",
"in": "formData",
"description": "Category Name",
"required": true,
"type": "string"
},
{
"name": "categoryDescription",
"in": "formData",
"description": "Category Description",
"type": "string"
},
{
"name": "categoryImage",
"in": "formData",
"description": "Category Image",
"type": "file"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ServiceResponse"
}
}
}
}
},
"/api/category/{id}": {
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"description": "Category Id",
"type": "string"
}
],
"get": {
"tags": ["Category"],
"summary": "Category Details based on Category Id",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ServiceResponse"
}
}
}
},
"put": {
"tags": ["Category"],
"summary": "Update Category API",
"parameters": [
{
"name": "categoryName",
"in": "formData",
"description": "Category Name",
"required": true,
"type": "string"
},
{
"name": "categoryDescription",
"in": "formData",
"description": "Category Description",
"type": "string"
},
{
"name": "categoryImage",
"in": "formData",
"description": "Category Image",
"type": "file"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ServiceResponse"
}
}
}
},
"delete": {
"tags": ["Category"],
"summary": "Delete Category API",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ServiceResponse"
}
}
}
}
}
},
"definitions": {
"Category": {
"required": ["categoryName", "_id"],
"properties": {
"_id": {
"type": "integer",
"uniqueItems": true
},
"categoryName": {
"type": "string"
},
"categoryDescription": {
"type": "string"
},
"categoryImage": {
"type": "string"
}
}
},
"ServiceResponse": {
"properties": {
"message": {
"type": "string"
},
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/Category"
}
}
}
}
}
}
请帮忙我该如何解决这个问题?
module.exports = upload.single("categoryImage");
在上面的代码中只需导出
--upload
这是上面代码的更新代码
module.exports = upload;