2016-04-05 27 views
2

Angular kullanarak bir WebApi'den PDF indirmeye çalışıyorum ancak dosya sadece 15 bayt. Veri WebAPI den alınan giriş durumunda beklenen boyuttaAngular kullanarak PDF'yi indir

WebAPI

[HttpGet] 
    public HttpResponseMessage MatchRegistrationReport(int matchId) 
    { 
     try 
     { 
      var gen = new MSReports.Components.MatchRegistration(); 
      byte[] bytes = gen.GeneratePDF(matchId, 10); 
      var stream = new MemoryStream(bytes); 

      var result = new HttpResponseMessage(HttpStatusCode.OK) 
      { 
       Content = new StreamContent(stream) 
       //Content = new ByteArrayContent(bytes) 
      }; 
      result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
      { 
       FileName = gen.ReportName + ".pdf" 
      }; 
      result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); 
      return result; 
     } 
     catch (Exception ex) 
     { 
      Log.Error(ex.Message); 
      return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); 
     } 
    } 

Eğik kumanda

$scope.Print = function() { 
    $scope.message = "Downloading"; 
    reportResource.printMatchRegistration($scope.match.Id).then(function (data, status, headers, config) { 
     var file = new Blob([data], { 
      type: 'application/csv' 
     }); 
     //trick to download store a file having its URL 
     var fileURL = URL.createObjectURL(file); 
     var a = document.createElement('a'); 
     a.href = fileURL; 
     a.target = '_blank'; 
     a.download = 'MatchRegistration.pdf'; 
     document.body.appendChild(a); 
     a.click(); 
     //$scope.message = "Completed"; 
    }, function (data, status, headers, config) { 
     $scope.message = "A error occurred"; 
    }); 
} 

ve kaynak ile arraybuffer var

printMatchRegistration: function (matchId) { 
    return $http({ 
    method: 'get', 
    url: this.getApiPath() + "MatchRegistrationReport?matchId=" + matchId, 
    headers: { 
     'Content-type': 'application/pdf', 
    }, 
    responseType: 'arraybuffer' 
}); 

İçerik türüyle ilgili bir şey olduğuna inanıyorum ama ne olduğunu anlayabiliyorum.

cevap

2

Merhaba sadece cevap

bu

reportResource.printMatchRegistration($scope.match.Id).then(function (response) { 
    var file = new Blob([response.data], { 
     type: 'application/pdf' 
    }); 

için Değiştir ve bu

 printMatchRegistration: function (matchId) { 
      var data = { 'matchId': matchId }; 
      return $http({ 
       method: 'get', 
       url: this.getApiPath() + "MatchRegistrationReport", 
       params: data, 
       headers: { 
        'Content-type': 'application/pdf', 
       }, 
       responseType: 'arraybuffer' 
      }); 
     }, 
bulundu