简体   繁体   English

使用多个键上传POSTMAN文件

[英]POSTMAN file upload with multiple keys

Hi i am developing multiple file upload module in angularjs as fron end. 嗨,我正在开发angularjs中的多个文件上传模块。 I am using FormData to send file. 我正在使用FormData发送文件。 Hence it is multiple file upload i am looping through files and appending file to FormData. 因此,这是多个文件上传,我正在循环浏览文件并将文件附加到FormData。 Along with this i am sending one more extra params as well. 与此同时,我还要发送一个额外的参数。

 var files = new FormData();
        angular.forEach(this.pendingFiles, function (value, index) {
            files.append(index, value);
            files.append('data', angular.toJson(index).replace(/['"]+/g, ''));
        });
  return $http.post(this.uploadUrl, files, {
            transformRequest: angular.identity,
            headers: {
                'Content-Type': undefined
            }
        })

Below is my directive 以下是我的指令

myapp.directive('fileModel', ['fileUpload', function (fileUpload) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            element.bind("change", function (evt) {
                fileUpload.pendingFiles[attrs.fileModel] = evt.target.files[0];
            });
        }
    };
}]);

Above code works fine when i run angularjs script. 当我运行angularjs脚本时,上面的代码工作正常。 I am trying to upload files using POSTMAN. 我正在尝试使用POSTMAN上传文件。 I am struggling here how can i send below data 我在这里苦苦挣扎如何发送以下数据

  files.append(index, value);
  files.append('data', angular.toJson(index).replace(/['"]+/g, ''));

normal file upload i know. 我知道正常的文件上传。 My problem is i have one extra params data. 我的问题是我有一个额外的参数数据。 I am not sure how can i send that via POSTMAN? 我不确定如何通过POSTMAN发送该邮件? Can someone help me in this regard? 有人可以在这方面帮助我吗? Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

In postman, set method type to POST. 在邮递员中,将方法类型设置为POST。

Then select Body -> form-data -> Enter your parameter name (file according to your code) For rest of "text" based parameters, you can post it like normally you do with postman. 然后选择“正文”->“表单数据”->“输入参数名称”(根据您的代码创建文件)对于其余基于“文本”的参数,您可以像使用邮递员一样进行张贴。 You can see in the image below for an example. 您可以在下面的图像中查看示例。 在此处输入图片说明

But probably you want to make the data key dynamic in your code. 但是可能您想使数据密钥在代码中动态化。 Otherwise if you are using the same key name with multiple values, then at server side it should be handled as an array 'data':['Passport','Visa'] 否则,如果您使用具有多个值的相同键名,则应在服务器端将其作为数组'data':['Passport','Visa']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM