的方法如下:
public ActionResult GetImage()
{
string imagePath = "path_to_image_file.jpg";
return new FilePathResult(imagePath, "image/jpeg");
}
public byte[] GetImageData()
{
ActionResult result = GetImage();
MemoryStream memoryStream = new MemoryStream();
result.ExecuteResult(new ControllerContext(), memoryStream);
byte[] imageData = memoryStream.ToArray();
return imageData;
}
public string ConvertToBase64(byte[] imageData)
{
string base64String = Convert.ToBase64String(imageData);
return base64String;
}
完整的代码示例如下:
public ActionResult GetImage()
{
string imagePath = "path_to_image_file.jpg";
return new FilePathResult(imagePath, "image/jpeg");
}
public byte[] GetImageData()
{
ActionResult result = GetImage();
MemoryStream memoryStream = new MemoryStream();
result.ExecuteResult(new ControllerContext(), memoryStream);
byte[] imageData = memoryStream.ToArray();
return imageData;
}
public string ConvertToBase64(byte[] imageData)
{
string base64String = Convert.ToBase64String(imageData);
return base64String;
}
// 调用示例
byte[] imageData = GetImageData();
string base64String = ConvertToBase64(imageData);
这样,你就可以从FilePathResult ASP.net MVC发送的http响应中将二进制图像数据转换为base64字符串了。
领取专属 10元无门槛券
手把手带您无忧上云