I'm using the same code as I've used successfully in a similar project for file uploads, yet for some reason the boundary never gets added to the content-type property in the request header. This results in my C# web-api function not being able to detect the image.
This is my post request using angularjs:
var formData = new FormData($('#testform')[0]);
$http({
url: serviceBase + 'api/Client/' + item.practiceID + '/SavePhoto',
method: "POST",
data: formData,
headers: { 'Content-Type': false },
transformRequest: function (data) { return data; }
}).success(function (response) {
}).error(function () {
});
This is what the request looks like:
Request URL:http://localhost:56769/api/Client/178/SavePhoto
Request Headers
Provisional headers are shown
Accept:application/json, text/plain, */*
Authorization:Bearer JfDnrAIkSttZ8GnHa-Wo9wBH-HVWpbiUgGSic11DF_OlTgseuTPgTisxybvEyw2fEyer1FJ7DjKWqK15P-ZdhO_X1aHp7-GiIW2Q4BTF5svTyJKWtM2jk-XEN6qXuEIhAi6-phryd_LlGLOlLMYMKhQZGULxdyk_dUvDGt6bY5Z0L-LbV5uc74q3MyLMMj_vypNgbFCAxGEAGeeeGlP7jwlyyz7EY-eRfRhXxjFqOjI
Content-Type:false
Origin:http://localhost:58431
Referer:http://localhost:58431/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Request Payload
------WebKitFormBoundary6padOMi9aJxqx34h
Content-Disposition: form-data; name="profile-photo"; filename="Jellyfish.jpg"
Content-Type: image/jpeg
------WebKitFormBoundary6padOMi9aJxqx34h--
Does anyone know what might be causing the boundary to fail to generate in the content-type parameter?
For the most part, the main difference is the Content-Type of the different form fields or “Parameters”. The first form part should be “application/json” and the binary file type should be “application/pdf”.
multipart/form-data contains boundary to separate name/value pairs. The boundary acts like a marker of each chunk of name/value pairs passed when a form gets submitted. The boundary is automatically added to a content-type of a request header.
Multipart/form-data is ideal for sending non-ASCII or binary data, and is the only content type that allows you to upload files. For more information about form data, see http://www.w3.org/TR/html401/interact/forms.html. NOTE: In cURL, you specify each message part using the -F (or --form ) option.
The boundary parameter is a text string used to delineate one part of the message body from another. All boundaries start with two hyphens (--). The final boundary also concludes with two hyphens (--). The boundary can be made up of any ASCII character except for a space, a control character, or special characters.
Try passing the Content-Type
header as undefined
instead of false
:
headers: { 'Content-Type': undefined }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With