C# 在ASP.NET MVC中将Blob作为文件下载

C# 在ASP.NET MVC中将Blob作为文件下载,c#,asp.net,ajax,asp.net-mvc,C#,Asp.net,Ajax,Asp.net Mvc,我正在将一个文件作为blob保存到MySQL数据库中。你能帮助我吗,我怎样才能下载这个blob作为一个文件?我在数据库中有blob数据和ContentType。你可以在下面看到我的下载方法。我已经找了一个多星期了,但没能找到。我也不知道我可以通过这个方法直接下载,或者我需要编写ajax。我非常感谢你的帮助和帮助。非常感谢 方法: public HttpPostedFileBase Indir() { using (ISession session=FluentNHibernateHelp

我正在将一个文件作为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; }
}

非常感谢你的回答!我找到了解决方案,正在编写下面的代码块

public HttpPostedFileBase Indir(string documentId)
    {
       using (ISession session=FluentNHibernateHelper.OpenSession())
        {

            var doc = new Document();
            var docDet = new DocumentDetail();

            doc = session.Query<Document>().FirstOrDefault(x => x.Id == Convert.ToInt32(documentId));
            docDet = session.Query<DocumentDetail>().FirstOrDefault(x=>x.DocumentId==doc.Id);
            var test = new MemoryPostedFile(docDet.File,doc.DocumentName,doc.DocumentExtention);
            Response.Clear();
            Response.ContentType = doc.DocumentExtention;//"application/octet-stream";
            Response.AddHeader("Content-Disposition","attachment; filename="+test.FileName+";");
            Response.BinaryWrite(docDet.File);
            Server.MapPath("~/"+test.FileName);
            Response.End();
            return test;
        }
    }
public HttpPostedFileBase Indir(字符串documentId)
{
使用(ISession session=FluentNHibernateHelper.OpenSession())
{
var doc=新文档();
var docDet=新文档详细信息();
doc=session.Query().FirstOrDefault(x=>x.Id==Convert.ToInt32(documentId));
docDet=session.Query().FirstOrDefault(x=>x.DocumentId==doc.Id);
var test=newmemorypostedfile(docDet.File、doc.DocumentName、doc.documentextension);
Response.Clear();
Response.ContentType=doc.documentextension;//“应用程序/八位字节流”;
AddHeader(“内容处置”、“附件;文件名=“+test.filename+”;”);
BinaryWrite(docDet.File);
Server.MapPath(“~/”+test.FileName);
Response.End();
回归试验;
}
}

非常感谢您的回答!我找到了解决方案,正在编写下面的代码块

public HttpPostedFileBase Indir(string documentId)
    {
       using (ISession session=FluentNHibernateHelper.OpenSession())
        {

            var doc = new Document();
            var docDet = new DocumentDetail();

            doc = session.Query<Document>().FirstOrDefault(x => x.Id == Convert.ToInt32(documentId));
            docDet = session.Query<DocumentDetail>().FirstOrDefault(x=>x.DocumentId==doc.Id);
            var test = new MemoryPostedFile(docDet.File,doc.DocumentName,doc.DocumentExtention);
            Response.Clear();
            Response.ContentType = doc.DocumentExtention;//"application/octet-stream";
            Response.AddHeader("Content-Disposition","attachment; filename="+test.FileName+";");
            Response.BinaryWrite(docDet.File);
            Server.MapPath("~/"+test.FileName);
            Response.End();
            return test;
        }
    }
public HttpPostedFileBase Indir(字符串documentId)
{
使用(ISession session=FluentNHibernateHelper.OpenSession())
{
var doc=新文档();
var docDet=新文档详细信息();
doc=session.Query().FirstOrDefault(x=>x.Id==Convert.ToInt32(documentId));
docDet=session.Query().FirstOrDefault(x=>x.DocumentId==doc.Id);
var test=newmemorypostedfile(docDet.File、doc.DocumentName、doc.documentextension);
Response.Clear();
Response.ContentType=doc.documentextension;//“应用程序/八位字节流”;
AddHeader(“内容处置”、“附件;文件名=“+test.filename+”;”);
BinaryWrite(docDet.File);
Server.MapPath(“~/”+test.FileName);
Response.End();
回归试验;
}
}

可能存在的重复项。另请参见:正确的方法是创建HTTP处理程序。名称中带有“Posted”的类用于从用户上载的文件。您想让用户下载soemthing。IHTTPHanlder很可能就是您正在寻找的droid。请注意,既然您在问题中提到了它,您就无法通过Ajax下载文件,因为可能存在重复的。另请参见:正确的方法是创建HTTP处理程序。名称中带有“Posted”的类用于从用户上载的文件。您想让用户下载soemthing。IHTTPHanlder很可能就是您正在寻找的droid。请注意,因为您在问题中提到了它,所以无法通过AJAX下载文件