我可以创建一个简单地返回图像资产的控制器吗?
我想通过控制器路由这个逻辑,每当请求如下URL时:
www.mywebsite.com/resource/image/topbanner
控制器将查找topbanner.png并将图像直接发送回客户端。
我见过这样的例子,你必须创建一个视图-我不想使用视图。我想只用控制器来做。
这可能吗?
我可以创建一个简单地返回图像资产的控制器吗?
我想通过控制器路由这个逻辑,每当请求如下URL时:
www.mywebsite.com/resource/image/topbanner
控制器将查找topbanner.png并将图像直接发送回客户端。
我见过这样的例子,你必须创建一个视图-我不想使用视图。我想只用控制器来做。
这可能吗?
当前回答
查看ContentResult。这将返回一个字符串,但可用于创建您自己的类binaryresult。
其他回答
你可以使用文件返回一个文件,如视图,内容等
public ActionResult PrintDocInfo(string Attachment)
{
string test = Attachment;
if (test != string.Empty || test != "" || test != null)
{
string filename = Attachment.Split('\\').Last();
string filepath = Attachment;
byte[] filedata = System.IO.File.ReadAllBytes(Attachment);
string contentType = MimeMapping.GetMimeMapping(Attachment);
System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
{
FileName = filename,
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(filedata, contentType);
}
else { return Content("<h3> Patient Clinical Document Not Uploaded</h3>"); }
}
查看ContentResult。这将返回一个字符串,但可用于创建您自己的类binaryresult。
稍微解释一下迪兰的回应:
有三个类实现了FileResult类:
System.Web.Mvc.FileResult
System.Web.Mvc.FileContentResult
System.Web.Mvc.FilePathResult
System.Web.Mvc.FileStreamResult
它们都是不言自明的:
For file path downloads where the file exists on disk, use FilePathResult - this is the easiest way and avoids you having to use Streams. For byte[] arrays (akin to Response.BinaryWrite), use FileContentResult. For byte[] arrays where you want the file to download (content-disposition: attachment), use FileStreamResult in a similar way to below, but with a MemoryStream and using GetBuffer(). For Streams use FileStreamResult. It's called a FileStreamResult but it takes a Stream so I'd guess it works with a MemoryStream.
下面是一个使用内容处理技术的例子(未测试):
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetFile()
{
// No need to dispose the stream, MVC does it for you
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", "myimage.png");
FileStream stream = new FileStream(path, FileMode.Open);
FileStreamResult result = new FileStreamResult(stream, "image/png");
result.FileDownloadName = "image.png";
return result;
}
读取图像,将其转换为byte[],然后返回具有内容类型的File()。
public ActionResult ImageResult(Image image, ImageFormat format, string contentType) {
using (var stream = new MemoryStream())
{
image.Save(stream, format);
return File(stream.ToArray(), contentType);
}
}
}
以下是用法:
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.AspNetCore.Mvc;
这对我很管用。 因为我将图像存储在SQL Server数据库上。
[HttpGet("/image/{uuid}")]
public IActionResult GetImageFile(string uuid) {
ActionResult actionResult = new NotFoundResult();
var fileImage = _db.ImageFiles.Find(uuid);
if (fileImage != null) {
actionResult = new FileContentResult(fileImage.Data,
fileImage.ContentType);
}
return actionResult;
}
在上面的代码片段中,_db.ImageFiles.Find(uuid)正在db (EF上下文)中搜索图像文件记录。它返回一个FileImage对象,它只是一个我为模型制作的自定义类,然后将其用作FileContentResult。
public class FileImage {
public string Uuid { get; set; }
public byte[] Data { get; set; }
public string ContentType { get; set; }
}