Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have mvc & angular application. I want to post image file to my web api.

my web api controller is:

  public class UploadController : ApiController
{
    public HttpResponseMessage Post()
    {
        HttpResponseMessage result = null;
        var httpRequest = HttpContext.Current.Request;
        if(httpRequest.Files.Count > 0)
        {
            var docfiles = new List<string>();
            foreach (string file in httpRequest.Files)
            {
                var postedFile = httpRequest.Files[file];
                var filePath = HttpContext.Current.Server.MapPath("~/"+postedFile.FileName);
                postedFile.SaveAs(filePath);
                docfiles.Add(filePath);
            }
            result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
        }
        else
        {
            result = Request.CreateResponse(HttpStatusCode.BadRequest);
        }
        return result;

    }
}

when im performing a request to post file via Postman i use this :

Headers = { Authorization : bearer ... , enctype : multipart/form-data }

and and the body request is form-data with key value pair (string , file), and this is successfully uploading my image.

what i try to send the image through my application (via angular $http):

var input = document.getElementById('uploadproductimage'); // FileList object
    UploadImageFactory(input.files[0])

and this is my factory code:

    return function (image) {
    var result = $q.defer();
    var formData = new FormData();

    if (image) {
        formData.append("uploadFile", image);
    }

    $http({
        method: 'POST',
        url: SessionService.apiUrl + '/api/upload',
        data: formData,
        header: { 'Authorization': 'Bearer ' + SessionService.getToken(), 'enctype': 'multipart/form-data' }
    })
    .success(function (response) {
        console.log(response);
        result.resolve(response);
    })  
    .error(function (response) {
        console.log(response);
        result.reject(response);
    });
    return result.promise;
}

and my html input:

                    <input type="file" id="uploadproductimage" name="uploadproductimage" class="input-xlarge">

Thanks!

share|improve this question
1  
Have you used something like Fiddler to determine the difference in what Postman is sending vs what Angular is posting? Also, is your getToken request asynchronous? – Mike Lawson May 17 at 21:42
    
angular request still got "Content-Type: application/json;charset=utf-8". and in postman request: "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvaPSgvkOAxWheGp5" – L.E. May 17 at 22:34
    
about 'getToken' - it isnt request its already stored in angular service. The token is received before the client would upload any file. – L.E. May 17 at 22:46
    
Ah. You might want to add a new Content-Type to your header tags if you're uploading a file. Try replicating the same values as the Postman request to see if they succeed. – Mike Lawson May 17 at 23:06
    
Thats what i say, i used the same requests (or i missing something). – L.E. May 18 at 4:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.