我正在使用此指令( ng--file-upload )上传图像。
我需要将图像文件包含到 ASP.NET Web API 的 POST 中,但没有成功。
这是我在服务器上的方法:

public async Task<HttpResponseMessage> SaveData([FromBody]PostModel data) 

当我发布数据(来自 js Angular )时,除了上传的文件之外,所有数据都在这里:

$http({ 
            method: 'POST', 
            url: path, 
            data: data, 
            file: photoFile 
        }).then(function... 

我也尝试将其放入数据中,但它始终为空。
如何在 Web api 上发布和获取图像文件?

作为我得到的回应:

"Invalid 'HttpContent' instance provided. It does not have a content type header starting with 'multipart/'. 
 Parameter name: content" 

在这一行:

var provider = new MultipartMemoryStreamProvider(); 
            await Request.Content.ReadAsMultipartAsync(provider); 
 
var data = {             
            "Username": $scope.username, 
            transformRequest: angular.identity, 
            headers: { 'Content-Type': undefined } 
        }; 
 
        var fd = new FormData(); 
        fd.append('file', $scope.photoFile); 
 
        $http({ 
            method: 'POST', 
            url: 'path', fd,  
            data: data 
        }).the 

请您参考如下方法:

使用FormData API :

html:

<div class="button" ngf-select="upload($file)">Upload on file select</div> 

Controller :

 $scope.upload = function (file) { 
    var fd = new FormData(); 
    fd.append('file', file); 
 
    //Append other data 
    //angular.forEach(data,function(v,k){ 
    //    fd.append(k,v); 
    //}); 
 
    $http.post('your-upload-url', fd, { 
        transformRequest: angular.identity, 
        headers: {'Content-Type': undefined} 
    }) 
    .then(function(res){ 
        // get upload res 
    }); 
}; 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!