Sun
Sun

Reputation: 4718

Upload file from angular to API

I'm trying to upload a file via angular.

So far I can select the file and post it from the angular end but I can't get the controller to receive the file.

this is how I post the file:

var f = new FormData();
f.append("file", file);

$http.post("/LoadFile", f); 

but I can't seem to get my controller to receive the file. I've tried all the following:

[HttpPost]
public ActionResult Upload(object file)

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)

[HttpPost]
public ActionResult Upload(HttpRequestMessage request)

[HttpPost]
public ActionResult Upload()

If I add breakpoint to the methods the only one that gets hit is the last one.

Upvotes: 0

Views: 155

Answers (1)

Sun
Sun

Reputation: 4718

Sorted.

I just needed to add the following the post method:

$http.post("/LoadFile", f, {
                transformRequest: angular.identity,
                headers: { 'Content-Type': undefined }
            }); 

Upvotes: 1

Related Questions