使用从asp.net打开下载的文件网站错误消息已损坏?

时间:2017-03-31 14:44:36

标签: c# asp.net sql-server download webforms

我们在使用asp.net Web Form创建的Intranet网站中有一个错误。当用户下载Office程序文件(如.docx,.pptx等)附件时,会发生错误,并尝试打开该文件,该文件将被破坏。

首先,我认为该程序来自代码,因为我创建了独立的应用程序,使用此博客Save and Retrieve Files from SQL Server Database using ASP.Net从sqlServer上传和下载文件。独立应用程序工作正常,但当我将代码移植到主应用程序时,仍然会发生错误。

最后,我试着玩电脑和IE坐着使MS办公室程序是办公室相关文件的默认应用程序但没有用。 有人有这个bug的解决方案吗?

[
  {
    "code": 4210,
    "msg": "Expression must be a group key or aggregate: (select (`b1`.`price`), (`b1`.`provider`), (`b1`.`bookingUrl`) from `bucket` as `b1` use keys (`b`.`offerIdentifier`) where ((`b1`.`flightCombinationIdentifier`) = (`b`.`flightCombinationIdentifier`)))  order by (`b1`.`price`) limit 1) as `offer`",
    "query_from_user": "select b.flightCombinationIdentifier,\n    (\n        select b1.price, b1.provider, b1.bookingUrl from bucket b1\n        use keys b.offerIdentifier\n        where b1.flightCombinationIdentifier = b.flightCombinationIdentifier\n        order by b1.price asc\n        limit 1\n    ) as offer\n    \nfrom bucket w\nwhere b.flyFrom = 'HAM' and b.flyTo = 'LGW'\ngroup by b.flightCombinationIdentifier"
  }
]

行哈德斯

public partial class DownloadAttachment : IntranetPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = 0;
        if (int.TryParse(Page.Request.QueryString["RecordID"], out id))
        {
            if (id > 0)
            {
                //GetDocument(id);
                RetreiveFileFormDatabase(id);
            }
        }
        Page.Response.End();


    }



    protected void GetDocument(int documentID)
    {
        try
        {
            Document downloadDoc = Document.GetDocument(documentID);
            DocumentAttachment downloadAttachment =
                DocumentAttachment.GetDocumentAttachment(downloadDoc.DocumentAttachmentID);
            DocumentType downloadType = DocumentType.GetDocumentType(downloadAttachment.DocumentTypesID);
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
            Response.ContentType = downloadType.ContentType;
            Response.AddHeader("content-disposition", "attachment;filename=" + downloadAttachment.FileName);
            Response.AddHeader("Content-Length", downloadAttachment.File.Length.ToString());
            Response.BinaryWrite(downloadAttachment.File.ToArray());

            Response.Flush();
            Response.Close();
            Response.End();


        }
        catch (Exception ex)
        {

        }
        finally
        {
            Response.End();
        }

    }

    protected void RetreiveFileFormDatabase(int id)
    {
        string strQuery = @"SELECT  DA.[RecordID],DA.[FileName],DA.[DocumentTypesID],DA.[Path],DA.[File],DA.[Deleted],DA.[ModifiedBy],DA.[ModifiedOn],DA.[CreatedBy],DA.[CreatedOn],DA.[Timestamp],DT.[ContentType]
                            FROM [tblDocument_Attachments] DA
                            INNER JOIN [tblDocument_Types] DT
                            ON DA.[DocumentTypesID] = DT.[RecordID]
                            WHERE DA.[RecordID] = @id";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        DataTable dt = GetData(cmd);
        if (dt != null)
        {
            Download(dt);
        }
    }

    private void Download(DataTable dt)
    {
        Byte[] bytes = (Byte[])dt.Rows[0]["File"];
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = dt.Rows[0]["ContentType"].ToString();
        Response.AddHeader("content-disposition", "attachment;filename="
        + dt.Rows[0]["FileName"].ToString());
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }

    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }

}

0 个答案:

没有答案