I'm facing an issue with uploading a javascript file
or transmitting a javascript file
to a post function. On the client side, I am using angularjs
$http
service
to send the data as follows:
$http({
method: "POST",
url: "/api/fileupload",
data: JSON.stringify(scope.c.model), // where scope.c.model is the javascript File
contentType: "application/json"
}).then(successResponse => {
this.scope["text"] = "Success...";
}, errorResponse=> {
this.scope["text"] = "Error...";
});
Here is the web-api
controller
:
public void Post(HttpPostedFile file)
{
//do stuff...
}
The issue is that 'file' is null while scope.c.model contains the correct data. When I change the type of data to an array, it works fine. It seems like the post method is not recognizing the file.
The second approach below also does not work, as file.Count
returns zero.
public void Post()
{
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
//do stuff...
}
}