需要显示从 WebApi 调用中检索到的图像

Need to display an image retrieved from WebApi call

我需要像使用普通 HTML 一样显示图像,但出于安全原因,我无法为图像提供普通 url。相反,我需要从 WebApi 服务检索图像。我发现了这个:

而且,我查看了答案中提供的链接,但有些地方不起作用。我得到的只是缺少图像占位符。

这是我的代码 - 客户端:

     angular.element('#' + imageType + '_' + itemID).html('<img src="/api/filemanagermaindata/getFile?systemName=' + baseData.CustomerData.SystemName + '&fileID=' + id + '" />')

这是我的 WebApi 控制器方法

[HttpGet]
[Route("api/filemanagermaindata/getFile")]
public HttpResponseMessage GetFile(string systemName, int fileID)
{
    var customerData = ValidateUser(systemName, 0);
    var response = this.fileMover.GetFileDataHttpResponse(customerData.OrganizationID, fileID);
        return response;
}        

还有我的 class 获取和 returns 图像的方法...

var response = new HttpResponseMessage();

try
{
    FileManagerItem item = this.dataService.GetFileByID(fileID);
    var fullPath = this.rootLocation + Path.Combine( item.PhysicalPath, item.Name);

if (!File.Exists(fullPath))
{
    throw new Exception("Unable to locate the requested file");
}

var fileType = Path.GetExtension(item.Name).Replace(".", string.Empty);

if (ApplicationSettings.Instance.ImageFileExtensions.Contains(fileType))
{
    fileType = string.Format("image/{0}", fileType);
}

using (FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
{
    response = new HttpResponseMessage { Content = new StreamContent(fileStream) };
    response.Content.Headers.ContentType = new MediaTypeHeaderValue(fileType);
    response.Content.Headers.ContentLength = fileStream.Length;
};

return response;
}

愚蠢的错误。在加载数据之前,using {} 块正在终止 FileStream。