从ASHX将datatbase流文件返回给WebPage

时间:2016-12-01 19:55:57

标签: c# asp.net

我成功地将二进制流字段保存到数据库(pdf,jpg等)并在ASP.net Gridview中将其返回到我的网页:

  <asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="~/Formularios/file.ashx?ID={0}" HeaderText="Documento" 
            DataTextFormatString="{0}" Target="_top" Text="Download document" />

并在file.ashx

 public void ProcessRequest(HttpContext context)
{
    using (OficiosRecibidosDataContext db = new OficiosRecibidosDataContext())
    {
        int value = Convert.ToInt32(context.Request.QueryString["ID"]);

        OficiosRecibidosDocumentos padDoc = db.OficiosRecibidosDocumentos.SingleOrDefault(p => p.Id == value);

        if (padDoc == null) return;

        string strMIME = "";

        switch (padDoc.Extension.Trim())
        {
            case ".jpg":
                strMIME = "image/jpeg";
                break;
            case ".xls":
                strMIME = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                strMIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
            case ".doc":
                strMIME = "application/msword";
                break;
            case ".docx":
                strMIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                break;
            case ".pdf":
                strMIME = "application/pdf";
                break;
            default:
                strMIME = "application/octet-stream";
                break;
        }            

        context.Response.Clear();
        context.Response.AddHeader("content-disposition", "attachement filename=" + padDoc.NombreArchivo.Trim() + padDoc.Extension.Trim());
        context.Response.ContentType = strMIME;
        context.Response.BinaryWrite((byte[])padDoc.Contenido.ToArray());
        context.Response.End();            
    }        
}

public bool IsReusable {
    get {
        return false;
    }
}

这是我的记录

enter image description here

一切正常但是当我使用Firefox并点击下载文档时会出现一个“打开/另存为”对话框弹出,如果我选择“保存/为”,则下载的文件称为“file.ashx”。哪里错了?是不是context.Response.AddHeader假设我指的是输出文件的名称和扩展名?

1 个答案:

答案 0 :(得分:0)

尝试使用

CompileOutput::CompileOutput(QWidget *parent): QTextEdit(parent)
{
    setReadOnly(true);
}
CompileOutput::~CompileOutput()
{
}

void CompileOutput::mousePressEvent(QMouseEvent * event)
{
    QTextCursor tc = cursorForPosition ( event->pos());
    tc.select(QTextCursor::LineUnderCursor);
    QString strWord = tc.selectedText();

    if(!strWord.isEmpty())
    {

        emit selectedWord(strWord);
    }
}

void CompileOutput::enterEvent(QMouseEvent *event)
{
    QTextCursor tc = cursorForPosition(event->pos());
    tc.select(QTextCursor::LineUnderCursor);
    QString strWord = tc.selectedText();

    qDebug() << strWord;
    if(strWord=="the line i need.")
    {
        emit hoveredWord(); //this signal makes cursor shape change while over right line of text
    }

}

首先,你拼错了context.Response.AddHeader("content-disposition", "attachment; filename=\"" + padDoc.NombreArchivo.Trim() + padDoc.Extension.Trim() + "\"");

其次,它与文件名之间需要attachment

与您的错误无关,但如果您的文件名中有空格,则会产生截断的文件名。

相关问题