asp.net 415 formData POST

时间:2018-06-26 04:45:37

标签: angularjs form-data asp.net-apicontroller httppostedfilebase

问题:

API控制器无法访问HTTP请求中包含的FormData对象中的HTTP请求数据

我需要将附加在http Post请求中FormData Object中的文件对象数据传递给asp.net api控制器,前端是angularjs。我无法从api控制器检索http请求数据。我的代码如下。请调查一下,这将是非常好的:)

我通过时

  

内容类型:未定义

错误提示 415不支持的媒体类型

如果Content-type: multipart/form-data无法从API控制器访问数据。

前端

$scope.submit = function (files) {   
        var formData = new FormData();   

        var getFormData = function(appendFiles){
            if (appendFiles.length) {
                angular.forEach(appendFiles,function(file){
                    if(!file.uploaded){
                        formData.append("imgFiles",file);
                        file.uploaded = true;
                    }
                });            
            } else {
                formData.append('imgFiles', files);
            }
            console.log(formData.values());
            return formData;
        }

        $http({
            url : "URL",
            method: "POST",
            data: getFormData(files),
            headers: {
                'Content-Type': undefined

            },
            transformRequest: angular.identity,
        })
        .then(
            function (resp) {
                // alert(JSON.stringify(resp));
                console.log(resp)                
            },
            function (resp) {
                console.log(resp)
            }
        );
    };

Api控制器方法

        [HttpPost]
        [Route("route")]
        public string UploadFiles(List<HttpPostedFileBase> files)
        {
            var filesToDelete = HttpContext.Current.Request.Files;

            //i need to access file here.using param or otherway    

            return stat;
        }

1 个答案:

答案 0 :(得分:0)

  

我已经解决了这个问题。我已如下更改了api控制器方法,

        [HttpPost]
        [Route("route")]
        public async Task<string> UploadFiles()
        {
            FileUploadService fileService = new FileUploadService();
            if (!Request.Content.IsMimeMultipartContent())
            {
                this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
            }
            var uploadFolder = "upload folder physical path"

            //this method is in below
            var provider = GetMultipartProvider(uploadFolder);

            await Request.Content.ReadAsMultipartAsync(provider);

            foreach (MultipartFileData fileData in provider.FileData)
            {

                var physicalPath = fileData.LocalFileName;
                var fileName = fileData.Headers.ContentDisposition.FileName;

            }

            return "return value";
        }

        private MultipartFormDataStreamProvider GetMultipartProvider(string uploadFolder)
        {
            try
            {
                var root = HttpContext.Current.Server.MapPath(uploadFolder);

                if (!Directory.Exists(root))
                {
                    Directory.CreateDirectory(root);
                }
                return new MultipartFormDataStreamProvider(root);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }