【发布时间】:2015-09-29 20:15:00
【问题描述】:
我在 Stackoverflow 上查看了许多网站和许多页面,但它们都没有解决我的问题。简单地说,我有一个hyperlink,我想通过Ajax 调用从数据库中检索图像,然后将其显示在FancyBox 弹出窗口中。我还尝试了Javascript 和Controller 操作方法的许多不同组合,但没有管理,因此正确显示下载的文件。您能否看看我的代码并给出一个包含View 和Controller 中所有必要方法的工作示例?另一方面,最好为其他文件类型(即 excel、pdf)打开一个对话框,同时为图像文件打开 FancyBox。
查看:
<a onclick="downloadFile(@Model.ID);">@Model.FileName</a>
function downloadFile(id) {
$.ajax({
url: "/Issue/RenderImage?ID=" + id,
async: true,
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response) {
$('#fancybox-inner').html('<img height="200" width="250" src="data:image/png;base64,' + response + '" />');
}
});
}
Controller:Controller中的方法没有问题,可以正常返回图片。
[HttpPost]
public virtual JsonResult RenderImage(int id)
{
string str = System.Convert.ToBase64String(repository.FileAttachments.FirstOrDefault(p => p.ID == id).FileData, 0, repository.FileAttachments.FirstOrDefault(p => p.ID == id).FileData.Length);
return Json(new { Image = str, JsonRequestBehavior.AllowGet });
}
【问题讨论】:
标签: jquery ajax asp.net-mvc fancybox asp.net-ajax