Search code examples
angularjsasp.net-mvcasp.net-mvc-routingasp.net-web-api-routing

Angular Ng-File-Upload url cannot find MVC ApiController Method


I am trying to use ng-file-upload to upload files and save blob to database as in my previous question Ng-File-Upload to Save PDF Blob to Database

I believe that I have the "correct" code to complete the task, but the problem now is that I cannot get the code to "hit" the method in the ApiController. If I put the method in a regular async controller, it finds it (but then I can't use half of the code needed because it doesn't work unless it is in a controller that inherits from ApiController), but it won't find the same method in the ApiController. What am I missing?

Here is the Angular Controller Code:

angular.module('myModule').controller('DocumentCtrl', function ($scope, $interval, $window, $filter, $q, $timeout, Upload, $http) {
$scope.uploadPic = function (file) {
    file.upload = Upload.upload({
        url: '/SSQV4/SSQV5/Document/UploadEMRDocument',
        method: 'POST',
        data: { file: file }
    })
}

}) Here is the MVC Controller Code:

public class DocumentController : ApiController
{

    public async Task<IHttpActionResult> UploadEMRDocument()
    {

        try
        {
            var provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            var f = provider.Contents.First(); // assumes that the file is the only data

            if (f != null)
            {
                var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
                filename = Path.GetFileName(filename);
                var buffer = await f.ReadAsByteArrayAsync();

                //buffer now contains the file content,
                //and filename has the original filename that was uploaded

                //do some processing with it (e.g. save to database)
            }
            else
            {
                return BadRequest("Attachment failed to upload");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

        return Ok();

    }
}

I am assuming that this is some sort of routing issue, but I'm not sure. Any Assistance is greatly appreciated!


Solution

  • I needed to change the url to

    url: '/SSQV4/SSQV5/api/Document/UploadEMRDocument'
    

    Now it hits it. Thanks for your help Nava-Prev.Queti!