2017-02-23 11 views
1

MVCパートは、MVCコントローラからExcelファイルを返すと、POSTリクエストでangularJS

リターンファイル(ストリーム、 "アプリケーション/ vnd.ms-エクセル"、file.FileName)からダウンロードしてください。

私は上記の方法でMVCコントローラからファイルを返しています。

AngularJSコントローラ

$scope.ExcelValidate = function() { 
    if ($scope.files && $scope.files.length && $scope.selectedFileType != -1) { 
     busyIndicatorService.showBusy("Uploading data..."); 
     for (var i = 0; i < $scope.files.length; i++) { 
      var file = $scope.files[i]; 
      $scope.file = file.name; 
      Upload.upload({ 
       url: '/MasterExcelImport/ValidateExcelData', 
       fields: { 
        uploadType: $scope.selectedFileType.Key.Key 
       }, 
       file: file 
      }).progress(function (evt) { 
       console.log('percent: ' + parseInt(100.0 * evt.loaded/evt.total)); 
       $scope.file.progress = parseInt(100.0 * evt.loaded/evt.total); 
      }).success(function (data) { 
       busyIndicatorService.stopBusy(); 
       var blob = new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }); 
       var objectUrl = URL.createObjectURL(blob); 
       window.open(objectUrl); 
      }).error(function (data, status, headers, config) { 
       busyIndicatorService.stopBusy(); 
      }); 
     } 
    } 
}; 

戻るファイルが既に成功セクションに来ます。しかし、ダウンロード部分が失敗しました。

この問題を解決するのにお手伝いできますか?

おかげで、 Erandika Sandaruwan

+0

はHTTP呼び出し –

+0

は何のエラーメッセージの結果として、base64文字列を取得している角度Contoller方法あなたは? – georgeawg

答えて

0

MVC/WeApiコントローラーアクション

[HttpPost] 
    public HttpResponseMessage DownloadExcelFile() 
    { 
     var stream = new MemoryStream();//fill stream from data or use filestream 
     var result = Request.CreateResponse(HttpStatusCode.OK); 
     result.Content = new ByteArrayContent(stream.ToArray()); 
     result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); 
     result.Content.Headers.ContentDisposition.FileName = "SystemPerformance.xlsx"; 
     return result; 
    } 

ExampleService.DownloadExcelFile({}/*post data*/,/*success function*/ function (response) { 
     var headers = response.headers(); 
     var blob = new Blob([response.data], { type: headers['content-type'] }); 
     var link = document.createElement('a'); 
     link.href = window.URL.createObjectURL(blob); 
     link.download = "excelfile.xlsx"; 
     link.click(); 
    }, /*error function*/ function (err) { 

    }); 
関連する問題