bekommen kann ich AngularJS bin mit jemandem zu erlauben, eine Datei auf meiner Web-Service zu schreiben, die # verwendet c WebAPI 2.Posting Dateien WebApi2 AngularJS mit - nicht Dateinamen oder Header
self.AddDocument = function() {
var f = document.getElementById('documentFile').files[0];
var r = new FileReader();
r.onloadend = function(e) {
var data = e.target.result;
console.log('AddDocument data' + data);
var fd = new FormData();
fd.append('Attachment', f);
$http.post('api/Document/' + $routeParams.changeId + '/AddDocument',
fd, {
method: 'POST',
url: 'api/Document/' + $routeParams.changeId + '/AddDocument',
data: fd,
// I have tried application/x-www-form-urlencoded & application/octet-stream for the content type
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response) {
console.log(response.data);
});
};
r.readAsArrayBuffer(f);
};
}
Dieser Code funktioniert gut, soweit ich sagen kann, dass ich eine HttpRequestMessage in meinem Code durchkommen (siehe unten), aber es gibt keine Header, die durchlaufen werden - aber ich kann die Content-Disposition sehen, wenn ich ReadAsStringAsync
[HttpPost]
[Route("api/Document/{id}/AddDocument")]
public async Task<string> Add(HttpRequestMessage request)
{
var doc = await request.Content.ReadAsByteArrayAsync();
var values = Enumerable.Empty<string>();
// this is always null
var header = (request.Content.Headers.TryGetValues(name: "filename", values: out values))
? values.First()
: null;
// this will be the following
/*
* ------WebKitFormBoundaryQIUoHBmcWJTY9cJx
Content-Disposition: form-data; name="Attachment"; filename="IMG_8639.JPG"
Content-Type: image/jpeg
*/
var str = await request.Content.ReadAsStringAsync();
// code that will add the byte array, filename, etc to a database table...
return "success";
}
Wie ziehe ich den Dateinamen aus einer hochgeladenen Datei?