2016-10-25 26 views
1

我真的迷失在这种情况下,所以我接受任何提示。阅读AngularJs上的BLOB文件

一些事实:

什么需要做的事情:文件的下载和上传,并以JPG文件的情况下,客户端必须能够想象它的浏览器。

这BLOB文件可以是任何东西,因为据我所知,JPG,DOC,PDF ...

我有存储在我的数据库的现有的BLOB文件,我要读它在我的网站,这是AngularJs应用程序。

我的服务器端是一个带有实体框架的Web Api。

到目前为止,我已经实现向Angular App发送一个字节[]。但它显示为一堆奇怪的字符,如 。在这里,我已经失去了。

在我的C#类上,我说将接收BLOB文件的var是一个byte []。这甚至是正确的吗?

我相信我有编码问题,因为我无法在HTML上阅读它。我被告知数据库使用UTF-16。我相信Angular正在等UTF-8(我如何确认或配置它?)。但是我从来没有遇到过任何其他数据的编码问题,但当然,他们不是BLOB文件。

我的C#代码返回BLOB数据:

[HttpGet] 
    [Route("Download/{documentId}")] 
    public HttpResponseMessage Documents(int documentId) 
    { 
     var document = _unit.Document.Get(documentId); 



     var response = new HttpResponseMessage(HttpStatusCode.OK); 
     response.Content = new ByteArrayContent(document.BlobFile); 
     response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 
     response.Content.Headers.ContentDisposition.FileName = document.Name; 

     return response; 
    } 

我的角码,recieving数据:

$scope.download = function (documentId) { 
      $http.get('Api/Documents/Download/' + documentId) 
       .then(function (response) { 
        console.log(response); 
        $scope.test = response.data 
       }) 
     } 

在HTML,我想这样的读它,假设它是一个图像(不知道它是否是正确的方式),我需要在浏览器上阅读它的唯一情况,我只需要让它可下载(我不知道该怎么做,但只有一个问题):

<img ng-src="data:image/jpeg;base64,{{teste}}" id="photo-id" /> 

正如我之前说过的,我真的迷失了,并且接受了sugestions,并且我找不到任何可以帮助我的例子。

提前致谢!

回答

3

的C#代码:

[HttpPost] 
public string GetCalculatorResults() 
{ 

    byte[] imgArr = GetImageFromDataBase(); 

    // Here we are converting the byte array to Base64String 
    return Convert.ToBase64String(imgArr) 
} 

的HTML和AngularJS源代码应该是

<div ng-app="myApp" ng-controller="myCtrl"> 

    <img ng-src="data:image/jpeg;base64,{{ imageSrc }}" /> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope, $http) { 
    $scope.imageSrc = ""; 
    $http.get("someurl") 
    .then(function(response) { 
     $scope.imageSrc = response.data; 
    }); 
}); 
</script> 

我测试了上面的代码,它的工作。

+0

好吧,我最终找到了解决方案,而且它非常像这样。无论如何,这可能有助于某人。 – Guilherme