一直在寻找解决方案,但一段时间以来一直困扰于此问题,却不知道如何解决。
我能够将文件成功上传到本地驱动器,因为我可以看到在目标文件夹中创建的文件。但是,我从ctx得到的响应是404,没有任何内容。
这是我的HTML
<form action="http://localhost:3000/api/image/upload" enctype="multipart/form-data" method="POST">
<input type="file" name="myFile" />
<input type="hidden" name="incomingIP" value="10.20.30.40" />
<input type="submit" value="Upload a file" />
</form>
还有我的js(route.js)
import Router from 'koa-router';
import multer from 'koa-multer';
const router = new Router({ prefix: '/api/image' });
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, process.env.FILES_UPLOAD_DIR)
},
filename: function (req, file, cb) {
let fileFormat = (file.originalname).split(".");
cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
}
})
const upload = multer({ storage: storage })
router.post('/upload', upload.any(), async ctx =>{
console.log(ctx);
});
export default router;
这是印刷的ctx
{
request: {
method: 'POST',
url: '/api/image/upload',
header: {
host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '34019',
'cache-control': 'max-age=0',
origin: 'http://localhost:3000',
'upgrade-insecure-requests': '1',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary9rMTCRZ78y5G4146',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
'sec-fetch-user': '?1',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
referer: 'http://localhost:3000/api/image',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
cookie: ''
}
},
response: {
status: 404,
message: 'Not Found',
header: [Object: null prototype] {
'content-type': 'text/plain; charset=utf-8',
'content-length': '9'
}
},
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/api/image/upload',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
}
我如何获取ctx发送的文件和正文?
我想念什么吗?
任何帮助将不胜感激