2017-02-23 9 views
1

MVC ЧастьВозвращение первенствовать файл из контроллера MVC и загрузить его с angularJS в запросе POST

возврата файла (поток, "применение/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

вы получаете base64 строку в результате HTTP вызова –

+0

сообщение Какую ошибку являются вы получаете? – 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) { 

    }); 
Смежные вопросы