如何下载二进制的xml文件?

如何下载二进制的xml文件?,xml,download,webforms,Xml,Download,Webforms,我有一个叫做下载的链接,他需要在数据库中以二进制格式记录的XML文件的浏览器中下载它。我做错了什么 代码如下: protected void DownloadFile_Click(object sender, EventArgs e) { int invoiceId = int.Parse((sender as LinkButton).CommandArgument); InvoiceManager iv = new InvoiceManager();

我有一个叫做下载的链接,他需要在数据库中以二进制格式记录的XML文件的浏览器中下载它。我做错了什么

代码如下:

 protected void DownloadFile_Click(object sender, EventArgs e)
    {
        int invoiceId = int.Parse((sender as LinkButton).CommandArgument);

        InvoiceManager iv = new InvoiceManager();
        Invoice invoice = iv.Find(invoiceId);
        if (invoice != null)
        {
            byte[] fileInBytes = invoice.FileContent;

            // Send the XML file to the web browser for download.
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "text/xml";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + invoice.FileName);
            Response.BinaryWrite(fileInBytes);
            Response.End();

        }
    }

如果您的意思是它显示在浏览器中而不是提示下载,请尝试将ContentType从text/xml更改为

protected void DownloadFile_Click(object sender, EventArgs e)
{
    int invoiceId = int.Parse((sender as LinkButton).CommandArgument);

    InvoiceManager iv = new InvoiceManager();
    Invoice invoice = iv.Find(invoiceId);
    if (invoice != null)
    {
        byte[] fileInBytes = invoice.FileContent;

        // Send the XML file to the web browser for download.
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + invoice.FileName);
        Response.BinaryWrite(fileInBytes);
        Response.End();

    }
}
这样,您将强制浏览器提示下载文件,而不是尝试在浏览器窗口中显示文件