mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
added support to properly handle file upload POST
This commit is contained in:
parent
210596eae9
commit
1cd77e2f14
@ -254,6 +254,7 @@
|
|||||||
method = $(this).attr('method'),
|
method = $(this).attr('method'),
|
||||||
self = this,
|
self = this,
|
||||||
params = {},
|
params = {},
|
||||||
|
formData = new FormData(),
|
||||||
headers = {},
|
headers = {},
|
||||||
content = $(this).find('textarea.content').val(),
|
content = $(this).find('textarea.content').val(),
|
||||||
result_container = $('.result', $(this).parent());
|
result_container = $('.result', $(this).parent());
|
||||||
@ -268,6 +269,7 @@
|
|||||||
var requestFormatMethod = '{{ requestFormatMethod }}';
|
var requestFormatMethod = '{{ requestFormatMethod }}';
|
||||||
if (requestFormatMethod == 'format_param') {
|
if (requestFormatMethod == 'format_param') {
|
||||||
params['_format'] = $('#request_format option:selected').text();
|
params['_format'] = $('#request_format option:selected').text();
|
||||||
|
formData.append('_format',$('#request_format option:selected').text());
|
||||||
} else if (requestFormatMethod == 'accept_header') {
|
} else if (requestFormatMethod == 'accept_header') {
|
||||||
headers['Accept'] = $('#request_format').val();
|
headers['Accept'] = $('#request_format').val();
|
||||||
}
|
}
|
||||||
@ -279,6 +281,37 @@
|
|||||||
headers['Content-type'] = 'application/'+bodyFormat;
|
headers['Content-type'] = 'application/'+bodyFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var hasFileTypes = false;
|
||||||
|
$('.parameters .tuple_type', $(this)).each(function() {
|
||||||
|
if ($(this).val() == 'file') {
|
||||||
|
hasFileTypes = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (hasFileTypes && method != 'POST') {
|
||||||
|
alert("Sorry, you can only submit files via POST.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasFileTypes) {
|
||||||
|
// retrieve all the parameters to send for file upload
|
||||||
|
$('.parameters .tuple', $(this)).each(function() {
|
||||||
|
var key, value;
|
||||||
|
|
||||||
|
key = $('.key', $(this)).val();
|
||||||
|
if ($('.value', $(this)).attr('type') === 'file' ) {
|
||||||
|
value = $('.value', $(this)).prop('files')[0];
|
||||||
|
} else {
|
||||||
|
value = $('.value', $(this)).val();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value) {
|
||||||
|
formData.append(key,value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// retrieve all the parameters to send
|
// retrieve all the parameters to send
|
||||||
$('.parameters .tuple', $(this)).each(function() {
|
$('.parameters .tuple', $(this)).each(function() {
|
||||||
var key, value;
|
var key, value;
|
||||||
@ -291,6 +324,10 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// retrieve the additional headers to send
|
// retrieve the additional headers to send
|
||||||
$('.headers .tuple', $(this)).each(function() {
|
$('.headers .tuple', $(this)).each(function() {
|
||||||
var key, value;
|
var key, value;
|
||||||
@ -312,7 +349,7 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// disable all the fiels and buttons
|
// disable all the fields and buttons
|
||||||
$('input, button', $(this)).attr('disabled', 'disabled');
|
$('input, button', $(this)).attr('disabled', 'disabled');
|
||||||
|
|
||||||
// append the query authentication
|
// append the query authentication
|
||||||
@ -340,9 +377,7 @@
|
|||||||
body = params;
|
body = params;
|
||||||
}
|
}
|
||||||
var data = content.length ? content : body;
|
var data = content.length ? content : body;
|
||||||
|
var ajaxOptions = {
|
||||||
// and trigger the API call
|
|
||||||
$.ajax({
|
|
||||||
url: endpoint + url,
|
url: endpoint + url,
|
||||||
type: method,
|
type: method,
|
||||||
data: data,
|
data: data,
|
||||||
@ -361,7 +396,18 @@
|
|||||||
// and enable them back
|
// and enable them back
|
||||||
$('input:not(.content-type), button', $(self)).removeAttr('disabled');
|
$('input:not(.content-type), button', $(self)).removeAttr('disabled');
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// overrides body format to send data properly
|
||||||
|
if (hasFileTypes) {
|
||||||
|
ajaxOptions.data = formData;
|
||||||
|
ajaxOptions.processData = false;
|
||||||
|
ajaxOptions.contentType = false;
|
||||||
|
delete(ajaxOptions.headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
// and trigger the API call
|
||||||
|
$.ajax(ajaxOptions);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
@ -241,7 +241,7 @@
|
|||||||
</p>
|
</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<button class="add_parameter">New parameter</button>
|
<button type="button" class="add_parameter">New parameter</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user