簡體   English   中英

無法將圖像文件從ng-file-upload上傳到Web API

[英]Can't upload image file from ng-file-upload to web api

我正在使用此指令( ng--file-upload )上傳圖像。
我需要將圖像文件包含在POST到ASP.NET Web API中,但是沒有任何成功。
這是我在服務器上的方法:

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

當我發布數據(從js角度)時,所有數據都在這里,除了上傳的文件:

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

我也嘗試將其放入數據中,但始終為null。
如何在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>

控制器:

 $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
    });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM