Skip to content

Commit

Permalink
add param File upload support (apidoc#850)
Browse files Browse the repository at this point in the history
* add param file upload support


Co-authored-by: js-obito <[email protected]>
  • Loading branch information
aqnaruto and js-obito authored Apr 25, 2020
1 parent addf62a commit 6811d58
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function postUser() { return; }
* @apiDescription This function has same errors like POST /user, but errors not defined again, they were included with "apiErrorStructure"
*
* @apiParam {String} name Name of the User.
* @apiParam {File} avatar Upload avatar.
*
* @apiUse CreateUserError
*/
Expand Down
2 changes: 1 addition & 1 deletion template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ <h4><input type="checkbox" data-sample-request-param-group-id="sample-request-pa
<div class="form-group">
<label class="col-md-3 control-label" for="sample-request-param-field-{{field}}">{{field}}</label>
<div class="input-group">
<input id="sample-request-param-field-{{field}}" type="text" placeholder="{{field}}" class="form-control sample-request-param" data-sample-request-param-name="{{field}}" data-sample-request-param-group="sample-request-param-{{@../index}}" {{#if optional}}data-sample-request-param-optional="true"{{/if}}>
<input id="sample-request-param-field-{{field}}" type="{{setInputType type}}" placeholder="{{field}}" class="form-control sample-request-param" data-sample-request-param-name="{{field}}" data-sample-request-param-group="sample-request-param-{{@../index}}" {{#if optional}}data-sample-request-param-optional="true"{{/if}}>
<div class="input-group-addon">{{{type}}}</div>
</div>
</div>
Expand Down
10 changes: 10 additions & 0 deletions template/utils/handlebars_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ define([
return text;
});

/**
* set paramater type.
*/
Handlebars.registerHelper("setInputType", function(text) {
if (text === "File") {
return "file";
}
return "text";
});

/**
* start/stop timer for simple performance check.
*/
Expand Down
14 changes: 10 additions & 4 deletions template/utils/send_sample_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ define([
// create JSON dictionary of parameters
var param = {};
var paramType = {};
var bodyFormData = {};
var bodyFormDataType = {};
var bodyFormData = new FormData();
var bodyJson = '';
$root.find(".sample-request-param:checked").each(function(i, element) {
var group = $(element).data("sample-request-param-group-id");
Expand All @@ -79,8 +78,10 @@ define([
}
if (contentType == "body-form-data"){
header['Content-Type'] = 'multipart/form-data'
bodyFormData[key] = value;
bodyFormDataType[key] = $(element).next().text();
if (element.type == "file") {
value = element.files[0];
}
bodyFormData.append(key,value);
}else {
param[key] = value;
paramType[key] = $(element).next().text();
Expand Down Expand Up @@ -134,6 +135,11 @@ define([
error : displayError
};

if(header['Content-Type'] == 'multipart/form-data'){
delete ajaxRequest.headers['Content-Type'];
ajaxRequest.contentType=false;
ajaxRequest.processData=false;
}
$.ajax(ajaxRequest);


Expand Down

0 comments on commit 6811d58

Please sign in to comment.