如何使用angularjs将文件数据发送到控制器方法?

时间:2019-01-22 12:23:17

标签: c# html angularjs asp.net-core-mvc

我的项目是Asp.net MVC Core。

我尝试将包含angularjs的文件上传到mvc控制器,但是我犯了一些错误;方法中,发送值是null。我可以写一个控制台日志,实际上数据不是null,但是问题正在发送到控制器。

angular.module("myApp").controller("myVm", function ($scope, $http, $window) {
    var vm = $scope;
    var url = "UploadSingle";
    var config = {
        headers: {
            "Content-Type": undefined,
        }
    };
    vm.upload = function () {

        var formData = new $window.FormData();
        formData.append("file-0", vm.files[0]);
        console.dir(formData);

        $http.post(url, formData, {
            transformRequest: angular.identity,
            headers: new HttpHeaders().set('enctype', 'multipart/form-data')
        }).
            then(function (response) {
                vm.result = "SUCCESS";
                vm.data = response.data.data;
            }).catch(function (response) {
                vm.result = "ERROR " + response.status;
            });
    };
       public async Task<JsonResult> UploadSingle(IFormFile formData) 
       {
           using (var reader = new StreamReader(formData.OpenReadStream())) 
           {
                var fileContent = reader.ReadToEnd();
                var parsedContentDisposition = ContentDispositionHeaderValue.Parse(formData.ContentDisposition);
                var fileName = parsedContentDisposition.FileName;

            }
            return Json(new { result = "Test" });
    }

0 个答案:

没有答案