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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:23:45  来源:igfitidea点击:

Upload a file with PUT/POST method on POSTMAN

javascriptnode.jsrequestpostman

提问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 :

在邮递员我得到这个:

postman

邮差

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尤其是当您测试文件上传时请确保,

  1. in Headers:
    • The Content-typefield has been set as multipart/form-datain Headers.
  2. in Body:
    • form-dataoption should be remain as default.
    • Choose Fileoption instead of textfrom dropdown at the right side.
    • Type Filein text box where placeholder is key.
  1. 标题中
    • Content-type字段已设置为multipart/form-data标题。
  2. 身体
    • 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.

第二天早上醒来又打了,它奏效了。下面是工作请求的图像。

enter image description here

在此处输入图片说明