Skip to content

Commit

Permalink
Merge pull request #1070 from swagger-api/fileLoader
Browse files Browse the repository at this point in the history
Submit file parameters with FormData
  • Loading branch information
saharj committed Sep 24, 2016
2 parents f96eefd + 4525ecd commit c8403bf
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions scripts/controllers/tryoperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ SwaggerEditor.controller('TryOperation', function($scope, formdataFilter,
if (!contentType) {
return bodyModel;

// if it has file parameters, body will be a FromData object
} else if (hasFileParam()) {
return makeFormDataFromFiles(bodyModel);

// if body has form-data encoding use formdataFilter to encode it to string
} else if (/form\-data/.test(contentType)) {
return formdataFilter(bodyModel);
Expand All @@ -720,10 +724,35 @@ SwaggerEditor.controller('TryOperation', function($scope, formdataFilter,
*/
function hasFileParam() {
return parameters.some(function(parameter) {
return parameter.format === 'file';
return parameter.type === 'file';
});
}

/**
* Make a FormData instance of all files in the parameters list
*
* @return {FormData} - FormData Object
*
*/
function makeFormDataFromFiles() {
var formData = new FormData();
parameters
.filter(function(parameter) {
return parameter.type === 'file';
})
.forEach(function(parameter) {
var fileInput = $('[data-schemapath="root.parameters"]' +
' [name="root[parameters][' + parameter.name + ']"]');
if (fileInput[0] && fileInput[0].files) {
var file = fileInput[0].files[0];
if (file) {
formData.append(parameter.name, file, file.name);
}
}
});
return formData;
}

/*
* Parse a HTTP response header string into hash of HTTP header key/values
* into
Expand Down Expand Up @@ -761,7 +790,8 @@ SwaggerEditor.controller('TryOperation', function($scope, formdataFilter,
type: $scope.operationName,
headers: _.omit($scope.getHeaders(), omitHeaders),
data: $scope.getRequestBody(),
contentType: $scope.contentType
contentType: $scope.contentType,
processData: false
})

.fail(function(jqXHR, textStatus, errorThrown) {
Expand Down

0 comments on commit c8403bf

Please sign in to comment.