如何通过 REST API .net return 二进制图像(不是 url)
How to return an image (not the url) in binary via a REST API .net
好的,我有一个网络api gets/adds/updates/deletes 客户,后端是 CosmosDB。
用户可以为每个客户上传图像,文件存储在 Azure Blob 存储中,但文件名存储在 CosmosDB 中 属性。
[HttpPost]
public async Task<IHttpActionResult> Adduser([FromBody]User user)
{
var telemetry = new TelemetryClient();
try
{
var userStore = CosmosStoreHolder.Instance.CosmosStoreUser;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//Then we validate the content type
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
#region File upload
//Initalize configuration settings
var accountName = ConfigurationManager.AppSettings["storage:account:name"];
var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
var profilepicturecontainername = ConfigurationManager.AppSettings["storage:account:profilepicscontainername"];
//Instance objects needed to store the files
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer imagesContainer = blobClient.GetContainerReference(profilepicturecontainername);
var provider = new AzureStorageMultipartFormDataStreamProvider(imagesContainer);
// Validate extension and image size
foreach (MultipartFileData file in provider.FileData)
{
var fileName = file.Headers.ContentDisposition.FileName.Trim('\"').Trim();
if (fileName.EndsWith(".png"))
{
var img = Image.FromFile(file.LocalFileName);
if (img.Width != 200 && img.Height != 200)
{
string guid = Guid.NewGuid().ToString();
return BadRequest($"Error Lulo. Unsupported extension, only PNG is valid. Or unsuported image dimensions (200px x 200px)");
}
}
}
//Try to upload file
try
{
await Request.Content.ReadAsMultipartAsync(provider);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest($"Error Lulo. An error has occured. Details: {guid} {ex.Message}: ");
}
// Retrieve the filename of the file you have uploaded
var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
if (string.IsNullOrEmpty(filename))
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
return BadRequest($"Error Lulo. An error has occured while uploading your file. Please try again.: {guid} ");
}
//Rename file
CloudBlockBlob blobCopy = imagesContainer.GetBlockBlobReference(user.Id + ".png");
if (!await blobCopy.ExistsAsync())
{
CloudBlockBlob blob = imagesContainer.GetBlockBlobReference(filename);
if (await blob.ExistsAsync())
{
await blobCopy.StartCopyAsync(blob);
await blob.DeleteIfExistsAsync();
}
}
#endregion
if (string.IsNullOrEmpty(user.CustomerId) && string.IsNullOrEmpty(user.PartnerId))
{
return BadRequest("ClientID or PartnerId must be filled in.");
}
var added = await userStore.AddAsync(user);
return Ok(added);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
现在,我需要 return 到网络 api,以某种方式前端开发人员可以呈现图像。
Azure Blob 容器不是 public,因此 returning Url 是不够的。
前端是react
这是我的获取方法(仅 return 图片 Url)
[HttpGet]
public async Task<IHttpActionResult> GetUser(string email)
{
var telemetry = new TelemetryClient();
try
{
var userStore = CosmosStoreHolder.Instance.CosmosStoreUser;
var roleStore = CosmosStoreHolder.Instance.CosmosStoreRole;
var user = await userStore.Query().FirstOrDefaultAsync(x => x.EmailAddress == email);
if (user == null)
{
return Unauthorized();
}
var role = await roleStore.Query().FirstOrDefaultAsync(x => x.Id == user.RoleId);
user.RoleName = role.RoleName;
return Ok(user);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
为了以防万一,下面的用户 POCO:
public class User : ISharedCosmosEntity
{
[JsonProperty("Id")]
public string Id { get; set; }
public string EmailAddress { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Enabled { get; set; }
public string ProfilePictureUrl { get; set; }
public string RoleName { get; set; }
public string CustomerName { get; set; }
public string PartnerName { get; set; }
public string CustomerId{ get; set; }
public string PartnerId { get; set; }
public string RoleId { get; set; }
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
}
您可以考虑使用以下方法将图像转换为 base64 字符串:https://www.c-sharpcorner.com/blogs/convert-an-image-to-base64-string-and-base64-string-to-image
我假设您要将图片嵌入元素中?
为什么不使用 Base64?然后你可以 return 像这样:
data:image/[png/jpg depending on image type];base64,[Your Base64 image value here]
在 Web API 中 return 文件的最佳方法是将其加载到内存中,然后通过 File 方法 return 它:
[HttpGet]
public IActionResult Get()
{
Byte[] b = ...; // Load blob from storage to byte array, usually via a MemoryStream.
return File(b, "image/jpeg");
}
File 方法确实是 return FileContentReult 或 FileStreamResult 的帮手,您可以在此处阅读更多相关信息:
如果您使用将流作为参数的 File 方法重载之一,您甚至可以避免在内存中缓冲整个 blob。
如果您没有常量 public url 图片,您需要将其编码为 base64 并将其作为内联图片嵌入。
如果您可以从 Web API 后端访问 POCO 中的 url,那么您可以使用以下代码检索和转换图像:
private static HttpClient _httpClient = new HttpClient();
public async Task<string> GetInlineImageSrcAsync(string url)
{
var bytes = await _httpClient.GetByteArrayAsync(url);
var base64 = Convert.ToBase64String(bytes);
var mimeType = "image/png";
// If mime types differ, try this
// var mimeType = $"image/{ParseExtensionFromUrl(url)}"
var inlineImageSrc = $"data:{mimeType};base64,{base64}";
return inlineImageSrc;
}
public string ParseExtensionFromUrl(string url)
{
return url.Substring(url.LastIndexOf(".") + 1);
}
请注意,HttpClient 应该是静态的,以使其能够重用连接。这是 Microsoft 推荐的,可以提高性能。
更多相关信息:
https://medium.com/@nuno.caneco/c-httpclient-should-not-be-disposed-or-should-it-45d2a8f568bc
https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
编辑:
这是实际嵌入图像的 HTML:
<img src="<THE STRING YOU RETURNED>" />
<!-- For Example: -->
<img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RqEkDx6T45jg13TJdp2SZijhuE3dC6zxuxA5VZos/eBPpH/BH79pCz8M/E3S9DN34u0RrWHOpW1rqltB4V1SPzIrdby6tpI90V4kcyhpIHTzvssUkuX85n/SD9tL9ivwn+2/8AClvDviRZbO+sna40jWLZR9q0mcjBZc8PGwwrxt8rgA/K6o6fk78af+CKPx3+GmtTQab4b0/4gaSjExaho97BGWQH5S9vcOkiuepVPNC8/Oep9DKM7y7NsojlmOqKnUilG70TSVk1dpPTdN3vquh5+cZHmWU5vLM8BTdSnJuVlq027tOybWuzStbR9UbP/BYn9oHxJ8TvjLo9kviPUbnwXqGjW+q2+mQXRbS/tInuoRMijht8EdvMA5YqtyGBAkUnA/4I1fBa8+Lf7eXhfUI4Hk0nwLFPrupy7TsjxE8NuhboHaeRGAPLLDLj7pIm+C//AARS+O/xM1uGHU/Den+ANKkYGXUNZvYJCqEncUt7d3ldx1Cv5Qb++vUfrF+xZ+xZ4T/Yg+FK+HPDay3l9eutxq+sXKj7Vq1wBgM2OEjUZVI1+VASfmdnd9M64gy/K8oeV4GpGpOUXG8dknu202r203buZ5Hw9mOaZus0x9OVOEZKVpXu2tUkmk7X8krH/9k=">
我在之前的一个项目中就是这样做的。我有一个采用图片的 Url 路径以及名称和扩展名的方法。该方法将图像转换为 base64 字符串。
public string ConvertToBase64(string path)
{
byte[] b = System.IO.File.ReadAllBytes(path);
var base64img = "data:image/jpg;base64," + Convert.ToBase64String(b);
Return base64img;
}
然后像这样使用它。
var imgUrl = AzureImgUrlPath + "imageName.jpg";
var imageBase64String = ConvertToBase64(imgUrl);
好的,我有一个网络api gets/adds/updates/deletes 客户,后端是 CosmosDB。
用户可以为每个客户上传图像,文件存储在 Azure Blob 存储中,但文件名存储在 CosmosDB 中 属性。
[HttpPost]
public async Task<IHttpActionResult> Adduser([FromBody]User user)
{
var telemetry = new TelemetryClient();
try
{
var userStore = CosmosStoreHolder.Instance.CosmosStoreUser;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//Then we validate the content type
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
#region File upload
//Initalize configuration settings
var accountName = ConfigurationManager.AppSettings["storage:account:name"];
var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
var profilepicturecontainername = ConfigurationManager.AppSettings["storage:account:profilepicscontainername"];
//Instance objects needed to store the files
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer imagesContainer = blobClient.GetContainerReference(profilepicturecontainername);
var provider = new AzureStorageMultipartFormDataStreamProvider(imagesContainer);
// Validate extension and image size
foreach (MultipartFileData file in provider.FileData)
{
var fileName = file.Headers.ContentDisposition.FileName.Trim('\"').Trim();
if (fileName.EndsWith(".png"))
{
var img = Image.FromFile(file.LocalFileName);
if (img.Width != 200 && img.Height != 200)
{
string guid = Guid.NewGuid().ToString();
return BadRequest($"Error Lulo. Unsupported extension, only PNG is valid. Or unsuported image dimensions (200px x 200px)");
}
}
}
//Try to upload file
try
{
await Request.Content.ReadAsMultipartAsync(provider);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest($"Error Lulo. An error has occured. Details: {guid} {ex.Message}: ");
}
// Retrieve the filename of the file you have uploaded
var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
if (string.IsNullOrEmpty(filename))
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
return BadRequest($"Error Lulo. An error has occured while uploading your file. Please try again.: {guid} ");
}
//Rename file
CloudBlockBlob blobCopy = imagesContainer.GetBlockBlobReference(user.Id + ".png");
if (!await blobCopy.ExistsAsync())
{
CloudBlockBlob blob = imagesContainer.GetBlockBlobReference(filename);
if (await blob.ExistsAsync())
{
await blobCopy.StartCopyAsync(blob);
await blob.DeleteIfExistsAsync();
}
}
#endregion
if (string.IsNullOrEmpty(user.CustomerId) && string.IsNullOrEmpty(user.PartnerId))
{
return BadRequest("ClientID or PartnerId must be filled in.");
}
var added = await userStore.AddAsync(user);
return Ok(added);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
现在,我需要 return 到网络 api,以某种方式前端开发人员可以呈现图像。 Azure Blob 容器不是 public,因此 returning Url 是不够的。
前端是react
这是我的获取方法(仅 return 图片 Url)
[HttpGet]
public async Task<IHttpActionResult> GetUser(string email)
{
var telemetry = new TelemetryClient();
try
{
var userStore = CosmosStoreHolder.Instance.CosmosStoreUser;
var roleStore = CosmosStoreHolder.Instance.CosmosStoreRole;
var user = await userStore.Query().FirstOrDefaultAsync(x => x.EmailAddress == email);
if (user == null)
{
return Unauthorized();
}
var role = await roleStore.Query().FirstOrDefaultAsync(x => x.Id == user.RoleId);
user.RoleName = role.RoleName;
return Ok(user);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
为了以防万一,下面的用户 POCO:
public class User : ISharedCosmosEntity
{
[JsonProperty("Id")]
public string Id { get; set; }
public string EmailAddress { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Enabled { get; set; }
public string ProfilePictureUrl { get; set; }
public string RoleName { get; set; }
public string CustomerName { get; set; }
public string PartnerName { get; set; }
public string CustomerId{ get; set; }
public string PartnerId { get; set; }
public string RoleId { get; set; }
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
}
您可以考虑使用以下方法将图像转换为 base64 字符串:https://www.c-sharpcorner.com/blogs/convert-an-image-to-base64-string-and-base64-string-to-image
我假设您要将图片嵌入元素中? 为什么不使用 Base64?然后你可以 return 像这样:
data:image/[png/jpg depending on image type];base64,[Your Base64 image value here]
在 Web API 中 return 文件的最佳方法是将其加载到内存中,然后通过 File 方法 return 它:
[HttpGet]
public IActionResult Get()
{
Byte[] b = ...; // Load blob from storage to byte array, usually via a MemoryStream.
return File(b, "image/jpeg");
}
File 方法确实是 return FileContentReult 或 FileStreamResult 的帮手,您可以在此处阅读更多相关信息:
如果您使用将流作为参数的 File 方法重载之一,您甚至可以避免在内存中缓冲整个 blob。
如果您没有常量 public url 图片,您需要将其编码为 base64 并将其作为内联图片嵌入。
如果您可以从 Web API 后端访问 POCO 中的 url,那么您可以使用以下代码检索和转换图像:
private static HttpClient _httpClient = new HttpClient();
public async Task<string> GetInlineImageSrcAsync(string url)
{
var bytes = await _httpClient.GetByteArrayAsync(url);
var base64 = Convert.ToBase64String(bytes);
var mimeType = "image/png";
// If mime types differ, try this
// var mimeType = $"image/{ParseExtensionFromUrl(url)}"
var inlineImageSrc = $"data:{mimeType};base64,{base64}";
return inlineImageSrc;
}
public string ParseExtensionFromUrl(string url)
{
return url.Substring(url.LastIndexOf(".") + 1);
}
请注意,HttpClient 应该是静态的,以使其能够重用连接。这是 Microsoft 推荐的,可以提高性能。 更多相关信息:
https://medium.com/@nuno.caneco/c-httpclient-should-not-be-disposed-or-should-it-45d2a8f568bc
https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
编辑: 这是实际嵌入图像的 HTML:
<img src="<THE STRING YOU RETURNED>" />
<!-- For Example: -->
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4QBoRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAMCAAIAAAAWAAAASlEQAAEAAAABAQAAAFERAAQAAAABAAAAAFESAAQAAAABAAAAAAAAAABQaG90b3Nob3AgSUNDIHByb2ZpbGUA/+IaxElDQ19QUk9GSUxFAAEBAAAatGFwcGwCEAAAbW50clJHQiBYWVogB90ACAACABEAMAAkYWNzcEFQUEwAAAAAAAAAAAA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5+cZHmWU5vLM8BTdSnJuVlq027tOybWuzStbR9UbP/BYn9oHxJ8TvjLo9kviPUbnwXqGjW+q2+mQXRbS/tInuoRMijht8EdvMA5YqtyGBAkUnA/4I1fBa8+Lf7eXhfUI4Hk0nwLFPrupy7TsjxE8NuhboHaeRGAPLLDLj7pIm+C//AARS+O/xM1uGHU/Den+ANKkYGXUNZvYJCqEncUt7d3ldx1Cv5Qb++vUfrF+xZ+xZ4T/Yg+FK+HPDay3l9eutxq+sXKj7Vq1wBgM2OEjUZVI1+VASfmdnd9M64gy/K8oeV4GpGpOUXG8dknu202r203buZ5Hw9mOaZus0x9OVOEZKVpXu2tUkmk7X8krH/9k=">
我在之前的一个项目中就是这样做的。我有一个采用图片的 Url 路径以及名称和扩展名的方法。该方法将图像转换为 base64 字符串。
public string ConvertToBase64(string path)
{
byte[] b = System.IO.File.ReadAllBytes(path);
var base64img = "data:image/jpg;base64," + Convert.ToBase64String(b);
Return base64img;
}
然后像这样使用它。
var imgUrl = AzureImgUrlPath + "imageName.jpg";
var imageBase64String = ConvertToBase64(imgUrl);