如何从asp.net C#sql sever数据库下载.exe文件

时间:2016-04-19 09:40:57

标签: c# asp.net sql-server-2008

Default.aspx的:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="download_code.aspx.cs" Inherits="download_code" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
public partial class download_code : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strQuery = "select Name, ContentType, Category, Data from Softwares where Id=@id";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = 19;
        DataTable dt = GetData(cmd);
        if (dt != null)
        {
            download(dt);
        }
    }
    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].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();
        }
    }
    private void download(DataTable dt)
    {
        byte[] bytes = (byte[])dt.Rows[0]["Data"];
        Response.Clear();
        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]["Name"].ToString());
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }
}

当我们使用此代码下载.exe文件时,它会以未知格式而不是.exe下载文件,我也尝试使用此代码下载zip文件,但它也没有成功

2 个答案:

答案 0 :(得分:1)

我认为“Content-Disposition”区分大小写,因此您应该尝试...

Response.AddHeader("Content-Disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString());

如果文件是zip,则尝试对内容类型进行硬编码,以确保它是正确的...

Response.ContentType = "application/zip";

如果文件是exe,请尝试

Response.ContentType = "application/octet-stream";

答案 1 :(得分:1)

尝试以下,

<a href="1.zip">Download Zip File</a>