私はバイナリイメージを取得することで、簡単なタスクを達成したい、と私のhtmlに表示戻りDBから画像および.NET APIとAngularJSと表示
パブリッククラスアートワーク
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid artworkID { get; set; }
public string artworkName { get; set; }
public string artworkMimeType { get; set; }
public byte[] artworkMeta { get; set; }
public string artworkBase64String { get; set; }
}
DB
public Artwork GetArtwork(Guid id)
{
return _context.Artworks.SingleOrDefault(a => a.artworkID == id);
}
APIコントローラからアートワークを取得します
public IHttpActionResult Get(Guid id)
{
if (id == null)
{
return BadRequest();
}
var artwork = _repository.GetArtwork(id);
if (artwork == null)
return NotFound();
else
return Ok(artwork);
}
私もこのメソッドを使用して、私が望むデータを返しますが、私はそれを使って目標を達成する方法をまだ分かりません。
[HttpGet]
public HttpResponseMessage Get(Guid id)
{
HttpResponseMessage result = null;
try
{
var artwork = _repository.GetArtwork(id);
if (artwork == null)
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{
// sendo file to client
byte[] bytes = artwork.artworkMeta ;
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new ByteArrayContent(bytes);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = artwork.artworkName;
}
return result;
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.Gone);
}
}
そして、ここで私の角度要求
$scope.getCity = function (id) {
$http.get('/api/artwork/' + $RouteParams.id).success(function (response) {
$scope.artwork= response;
//I've seen dudes using Blob here, but I'm not sure how that works
});
}
だ私の問題は、私はこれを行うことなく、アートワークを表示するのですか、私の角度の要求と私のhtmlです:
<img ng-src="data:{{artwork.artworkartworkMimeType}};base64,{{artwork.artworkBase64String}}" class="img-responsive" />
これは、画像を表示しますしかし、私はそれがどのように不器用なのが好きではない、私はオーディオファイルでも作業するつもりですので、私はきれいで理解可能な方法が必要です。助けてください!
にURLをバインドするためのURLは、その後に作成されるこれは完全に働いた、と私のコードをたくさんきれい。どうもありがとうございます!! –
素晴らしい! Np、喜んで:) –