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

I referred some Stack overflow Questions and I tried the following code. The following code was not hitting the Web API. I tried with normal Text, then its hitting but not for the FILE. Finally I concluded the Web API Path and method all are fine, only issue is File. Kindly assist me how to upload File using AngularJS to .NET Web API.

My HTML Code

<input id="file_input_file" ng-files="getTheFiles($files)" type="file" accept=".csv" />

<input type="button" data-ng-click="ImplortCSVData()" id="btnMainLoginForm" value="Upload File" />

My AngularJS Directive for ng-files is

app.directive('ngFiles', ['$parse', function ($parse) {

    function fn_link(scope, element, attrs) {
        var onChange = $parse(attrs.ngFiles);
        element.on('change', function (event) {
            onChange(scope, { $files: event.target.files });
        });
    };

    return {
        link: fn_link
    }
}]);

My AnagularJS Controller is

app.controller('ProcessCSVController', function ($rootScope, $scope, HTTPService) {

    $scope.CSVfile = {};

    $scope.getTheFiles = function ($files) {
        angular.forEach($files, function (value, key) {
            $scope.CSVfile.append(key, value);
        });
    };

    $scope.ImplortCSVData = function () {
        HTTPService.uploadCSV($scope.CSVfile).then(function (result) {
            alert("CSV");
        });
    }
});

My HTTPService is

app.factory('HTTPService', function ($http, $q) {
    formatCSV: function (_resultObj) {
        var result = $http({
            url: "localhost:8085/api/TAdmin/UploadCSVData",
            method: 'POST',
            data: _resultObj,
            headers: {
                "Content-Type": undefined
            },
        }).success(function (response) {
            return {
                errorCode: 0,
                errorString: "",
                result: response
            }
        }).error(function (error) {
            return {
                errorCode: 100,
                errorString: "",
                result: error
            }
        });
        return result;
    }
});

My WebAPI C# Code is

    [HttpPost]
    public HttpResponseMessage UploadCSVData(object csv)
    {

        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try
        {
            BinaryFormatter bf = new BinaryFormatter();
            using (var ms = new MemoryStream())
            {
                bf.Serialize(ms, csv);
                var x = ms.ToArray();
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }
share|improve this question

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.