Skip to content

Commit c8403bf

Browse files
authored
Merge pull request #1070 from swagger-api/fileLoader
Submit file parameters with FormData
2 parents f96eefd + 4525ecd commit c8403bf

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

scripts/controllers/tryoperation.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,10 @@ SwaggerEditor.controller('TryOperation', function($scope, formdataFilter,
696696
if (!contentType) {
697697
return bodyModel;
698698

699+
// if it has file parameters, body will be a FromData object
700+
} else if (hasFileParam()) {
701+
return makeFormDataFromFiles(bodyModel);
702+
699703
// if body has form-data encoding use formdataFilter to encode it to string
700704
} else if (/form\-data/.test(contentType)) {
701705
return formdataFilter(bodyModel);
@@ -720,10 +724,35 @@ SwaggerEditor.controller('TryOperation', function($scope, formdataFilter,
720724
*/
721725
function hasFileParam() {
722726
return parameters.some(function(parameter) {
723-
return parameter.format === 'file';
727+
return parameter.type === 'file';
724728
});
725729
}
726730

731+
/**
732+
* Make a FormData instance of all files in the parameters list
733+
*
734+
* @return {FormData} - FormData Object
735+
*
736+
*/
737+
function makeFormDataFromFiles() {
738+
var formData = new FormData();
739+
parameters
740+
.filter(function(parameter) {
741+
return parameter.type === 'file';
742+
})
743+
.forEach(function(parameter) {
744+
var fileInput = $('[data-schemapath="root.parameters"]' +
745+
' [name="root[parameters][' + parameter.name + ']"]');
746+
if (fileInput[0] && fileInput[0].files) {
747+
var file = fileInput[0].files[0];
748+
if (file) {
749+
formData.append(parameter.name, file, file.name);
750+
}
751+
}
752+
});
753+
return formData;
754+
}
755+
727756
/*
728757
* Parse a HTTP response header string into hash of HTTP header key/values
729758
* into
@@ -761,7 +790,8 @@ SwaggerEditor.controller('TryOperation', function($scope, formdataFilter,
761790
type: $scope.operationName,
762791
headers: _.omit($scope.getHeaders(), omitHeaders),
763792
data: $scope.getRequestBody(),
764-
contentType: $scope.contentType
793+
contentType: $scope.contentType,
794+
processData: false
765795
})
766796

767797
.fail(function(jqXHR, textStatus, errorThrown) {

0 commit comments

Comments
 (0)