Javascript 在 POSTMAN 上使用 PUT/POST 方法上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46282510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload a file with PUT/POST method on POSTMAN
提问by Cupkek05
I am trying to upload a file with POSTMAN to this url
我正在尝试使用 POSTMAN 将文件上传到此网址
http://localhost:3000/bucket/test/files/
And should got result in my controller there :
并且应该在我的控制器中得到结果:
put(request, response, args) {
//HERE IN THE REQUEST.BODY
console.log(request.body)
let fileManager = request.modules.VMFile;
let mimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/x-icon', ' video/mpeg', 'text/html', 'video/x-msvideo', 'application/msword', 'application/pdf', 'application/vnd.ms-powerpoint', 'application/x-rar-compressed'];
let maxFileSize = 4 * 1024 * 1024;
fileManager.initUpload(mimeTypes, maxFileSize);
fileManager.receive((files) => {
fileManager.forEachFileContent(files, (file, content) => {
minioClient.putObject(request.body.bucket, request.body.name, content, file.size, file.mimetype, function (err, etag) {
response.setData("File uploaded").apply();
return console.log(err, etag)
})
});
fileManager.clearFilesFromTmp(files);
});
}
In POSTMAN I got this :
在邮递员我得到这个:
With nothing on headers but I could only PUT (or POST, I tried to change my route with POST but same issue) the name and bucket field.. I got nothing on my files field..
标题上没有任何内容,但我只能 PUT(或 POST,我尝试使用 POST 更改我的路线但同样的问题)名称和存储桶字段.. 我的文件字段没有任何内容..
Any idea please ?
有什么想法吗?
回答by David R
While using Postmanespecially when you test file upload please ensure that,
使用时Postman尤其是当您测试文件上传时请确保,
- in Headers:
- The
Content-typefield has been set asmultipart/form-datain Headers.
- The
- in Body:
form-dataoption should be remain as default.- Choose
Fileoption instead oftextfrom dropdown at the right side. - Type
Filein text box where placeholder iskey.
- 在标题中:
- 该
Content-type字段已设置为multipart/form-data标题。
- 该
- 在身体:
form-data选项应保持为默认值。- 选择
File选项而不是text从右侧的下拉列表中。 File在占位符为 的文本框中键入key。
Hope this helps!
希望这可以帮助!
回答by Cupkek05
You might be doing it right but sometimes POSTMAN does not work well. I wrote an API to accept both text and file.
While invoking service from Postman.
1. I set Content-Type as "application/json" and Accept as "application/json".
2. In body I pass the text and file
It was not working, I tried multiple time. I shut down post man and my laptop.
您可能做得对,但有时 POSTMAN 效果不佳。我编写了一个 API 来接受文本和文件。
从 Postman 调用服务时。1. 我将 Content-Type 设置为“application/json”,将 Accept 设置为“application/json”。
2.在正文中我传递了文本和文件它不起作用,我尝试了多次。我关闭了邮递员和我的笔记本电脑。
Woke up next morning hit again and it worked. Below is image of working request.
第二天早上醒来又打了,它奏效了。下面是工作请求的图像。


