将 IRestResponse 转换为 "image/jpg" 文件
Converting IRestResponse to "image/jpg" File
我正在尝试通过 File()
方法将图像从 API 和 return 拉到 DOM。
这是我目前所拥有的..
HomeController.cs
:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult ImageFromPath()
{
var client = new RestClient("http://{{MYIPADDRESS}}/cgi-bin/snapshot.cgi?channel0=");
var request = new RestRequest(Method.GET);
request.AddHeader("postman-token", "random-postman-token");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Digest username=\"MYUSERNAME\", realm=\"MYENCRYPTEDPASS\", nonce=\"LONGSTRING\", uri=\"/cgi-bin/snapshot.cgi?channel0\", response=\"RESPONSESTRING\", opaque=\"\"");
IRestResponse response = client.Execute(request);(response.RawBytes);
return File(response, "image/jpg");
}
}
这里唯一的问题是 return 语句中的错误 response
显示
cannot convert from 'RestSharp.IRestResponse' to 'byte[]'
当我从本地文件系统中拉取图像时,它更容易工作,这是我 HomeController.cs
的工作代码
public ActionResult ImageFromPath(string path)
{
var ms = new MemoryStream();
using (Bitmap bitmap = new Bitmap(path))
{
var height = bitmap.Size.Height;
var width = bitmap.Size.Width;
bitmap.Save(ms, ImageFormat.Jpeg);
}
ms.Position = 0;
return File(ms, "image/jpg");
}
这是我在前端拉动它的方式 (Index.cshtml
):
<img src="@Url.Action("ImageFromPath", new { path = Request.MapPath("~/img/1.jpg") })" />
这里是这一行:
return File(response, "image/jpg");
您传递的是 response
类型 IRestResponse
(来自 RestSharp 的一种类型)。
为什么内置的 MVC 文件方法会知道 RestSharp? File()
takes a byte array and a string MIME type。
尝试:
return File(response.RawBytes, "image/jpg");
RawBytes
是来自您的 HTTP 请求的原始响应的字节数组。如果您的 API 返回图像的字节数组,这就是您需要传递给文件方法的内容。
我正在尝试通过 File()
方法将图像从 API 和 return 拉到 DOM。
这是我目前所拥有的..
HomeController.cs
:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult ImageFromPath()
{
var client = new RestClient("http://{{MYIPADDRESS}}/cgi-bin/snapshot.cgi?channel0=");
var request = new RestRequest(Method.GET);
request.AddHeader("postman-token", "random-postman-token");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Digest username=\"MYUSERNAME\", realm=\"MYENCRYPTEDPASS\", nonce=\"LONGSTRING\", uri=\"/cgi-bin/snapshot.cgi?channel0\", response=\"RESPONSESTRING\", opaque=\"\"");
IRestResponse response = client.Execute(request);(response.RawBytes);
return File(response, "image/jpg");
}
}
这里唯一的问题是 return 语句中的错误 response
显示
cannot convert from 'RestSharp.IRestResponse' to 'byte[]'
当我从本地文件系统中拉取图像时,它更容易工作,这是我 HomeController.cs
public ActionResult ImageFromPath(string path)
{
var ms = new MemoryStream();
using (Bitmap bitmap = new Bitmap(path))
{
var height = bitmap.Size.Height;
var width = bitmap.Size.Width;
bitmap.Save(ms, ImageFormat.Jpeg);
}
ms.Position = 0;
return File(ms, "image/jpg");
}
这是我在前端拉动它的方式 (Index.cshtml
):
<img src="@Url.Action("ImageFromPath", new { path = Request.MapPath("~/img/1.jpg") })" />
这里是这一行:
return File(response, "image/jpg");
您传递的是 response
类型 IRestResponse
(来自 RestSharp 的一种类型)。
为什么内置的 MVC 文件方法会知道 RestSharp? File()
takes a byte array and a string MIME type。
尝试:
return File(response.RawBytes, "image/jpg");
RawBytes
是来自您的 HTTP 请求的原始响应的字节数组。如果您的 API 返回图像的字节数组,这就是您需要传递给文件方法的内容。