1 回答
TA贡献1797条经验 获得超6个赞
<img src=...必须通过其路径指向图像文件,例如<img src="/myImage.jpg">。您不能将图像的二进制表示形式粘贴在其中src并使其正常工作。
因此,您可以将这些二进制图像写入磁盘的某个地方(您可能不想这样做,因为那时您正在复制数据,并且必须管理同步)。
或者您可以创建某种图像处理程序,因此<img src=类似于: <img src="/myHandler/imageId",然后让处理程序从数据库中读取二进制数据并以图像进行响应。
这是我过去用来从数据库中读取二进制 PDF 并将其作为文件返回的 MVC 控制器操作。这是在我的Competition控制器中。如果这是返回图像,您可以将其称为:
<img src="Competition/ViewJobDescription?competitionId=1234" />
public ActionResult ViewJobDescription(int competitionId)
{
string errorMsg = "";
var competition = new DBModel.Competition();
try
{
competition = DBModel.Competition.GetCompetition(competitionId);
if (competition != null && competition.AttachmentContent != null)
{
byte[] fileData = competition.AttachmentContent;
string filename = competition.AttachmentTitle + ".pdf";
return File(fileData, "application/pdf", filename);
}
}
catch (Exception ex)
{
errorMsg += "An error occured: " + ex.Message;
LogFile err = new LogFile();
err.CreateErrorLog(errorMsg);
ModelState.AddModelError(string.Empty, errorMsg);
}
return RedirectToAction("Index", "Home");
}
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报