Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 通过GridView从数据库下载文件时出现问题_Asp.net_Forms_Web - Fatal编程技术网

Asp.net 通过GridView从数据库下载文件时出现问题

Asp.net 通过GridView从数据库下载文件时出现问题,asp.net,forms,web,Asp.net,Forms,Web,我对文件有问题。我正在尝试从数据库下载文件 通过gridview,但下载后,它们的名称和扩展名都错误。它们的名称是我的web表单名称,扩展名是.aspx 在数据库中,它们以正确的格式保存 这是我的密码 public partial class data : System.Web.UI.Page { string cs = ConfigurationManager.ConnectionStrings["DecumentsConnectionString"].ConnectionString

我对文件有问题。我正在尝试从数据库下载文件 通过gridview,但下载后,它们的名称和扩展名都错误。它们的名称是我的web表单名称,扩展名是
.aspx

在数据库中,它们以正确的格式保存

这是我的密码

public partial class data : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["DecumentsConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillData();
        }
    }

    private void FillData()
    {
        DataTable dt = new DataTable();
        using (SqlConnection cn = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("getDecuments", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            dt.Load(reader);

            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        FileInfo filename = new FileInfo(FileUpload1.FileName);
        byte[] documentContent = FileUpload1.FileBytes;
        string name = filename.Name;
        string extnt = filename.Extension;

        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("savedocument", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@Content", documentContent);
            cmd.Parameters.AddWithValue("@extn",extnt);
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
    private void Donwload(int id)
    {
        DataTable dt = new DataTable();
        using (SqlConnection cn = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("getdocument", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ID", id);
            cn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            dt.Load(reader);
        }
        string name = dt.Rows[0]["name"].ToString();
        byte[] document = (byte[])dt.Rows[0]["DocumentContent"];
        Response.ClearContent();
        Response.ContentType = "application/octetstream";
        Response.AddHeader("Content-Dispisition", string.Format("attachment;filename={0}", name));
        Response.AddHeader("Content-Lenght", document.Length.ToString());
        Response.BinaryWrite(document);
        Response.Flush();
        Response.Close();
    }
        protected void OpenDocument(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            GridViewRow gr = (GridViewRow)lnk.NamingContainer;
            int id = int.Parse(GridView1.DataKeys[gr.RowIndex].Value.ToString());
            Donwload(id);
        }
    }
}
还有我的html

 <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Document"></asp:Label>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
                    <br />
                </td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
            <Columns>
                <asp:TemplateField HeaderText="Documents">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" OnClick="OpenDocument" runat="server" Text='<%# Eval("name") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
    </div>
</form>