【发布时间】:2019-04-05 18:10:23
【问题描述】:
我正在将一个文件作为 blob 保存到 MySQL 数据库中。你能帮我吗,我怎样才能把这个blob下载为文件?我在数据库中有 blob 数据和 ContentType。您可以在下面查看我的下载方法。我一直在寻找一个多星期,但我无法做到。我也不知道我可以直接通过方法下载还是我需要写ajax。我非常感谢您的帮助和帮助。非常感谢!
方法:
public HttpPostedFileBase Indir()
{
using (ISession session=FluentNHibernateHelper.OpenSession())
{
var doc = new Document();
var docDet = new DocumentDetail();
doc = session.Query<Document>().FirstOrDefault(x => x.Id == 5);
docDet = session.Query<DocumentDetail>().FirstOrDefault(x => x.DocumentId == doc.Id);
var test = new MemoryPostedFile(docDet.File, doc.DocumentName, doc.DocumentExtention);
return test;
}
}
类:
public class MemoryPostedFile:HttpPostedFileBase
{
private readonly byte[] fileBytes;
public MemoryPostedFile(byte[] fileBytes, string fileName = null, string ContentType = null)
{
this.fileBytes = fileBytes;
this.FileName = fileName;
this.ContentType = ContentType;
this.InputStream = new MemoryStream(fileBytes);
}
public override int ContentLength => fileBytes.Length;
public override string FileName { get; }
public override string ContentType { get; }
public override Stream InputStream { get; }
}
【问题讨论】:
-
正确的做法是做一个HTTP Handler。名称中带有“已发布”的类用于从用户上传的文件。你想给用户一些东西来下载。 IHTTPHanlder 可能是您正在寻找的机器人。
-
请注意,由于您在问题中提到了它,因此您无法通过 AJAX 下载文件
标签: c# asp.net ajax asp.net-mvc