C# 尝试使用gridview的linkbutton从数据库下载文件时出错
我试图使用gridview的linkbutton从数据库下载一个文件,但没有发生任何事情,因为它给了我一个错误,也没有下载该文件。 以下是我的实体: 我的数据库中有3个库号-SaleID和salefilename此库号存储文件及其名称,salename此列以字节存储相同的文件 以下是by.aspx代码:C# 尝试使用gridview的linkbutton从数据库下载文件时出错,c#,asp.net,C#,Asp.net,我试图使用gridview的linkbutton从数据库下载一个文件,但没有发生任何事情,因为它给了我一个错误,也没有下载该文件。 以下是我的实体: 我的数据库中有3个库号-SaleID和salefilename此库号存储文件及其名称,salename此列以字节存储相同的文件 以下是by.aspx代码: <asp:TemplateField HeaderText="SaleName" SortExpression="SaleName">
<asp:TemplateField HeaderText="SaleName" SortExpression="SaleName">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Bind("SaleFileName") %>' Text='<%# Bind("SaleName") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
当我们点击链接按钮下载文件时,网页没有给我任何回应,我是C语言的新手,任何人都可以告诉我哪里出错,这将是一个很大的帮助 是e.CommandName下载还是'SaleName?CommanName已下载,但您显示的链接按钮未下载,是吗?是的CommandName已下载。是否使用UpdatePanel!?
protected void gridContributions_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
// make sure fileName contains only file name like 'test.pdf'
string fileName = Convert.ToString(e.CommandArgument);
// make sure filePath contains file path like 'Uploads/Scheme/test.pdf'
string filePath = e.CommandArgument.ToString();
string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
// SqlConnection con = new SqlConnection(connectionString);
byte[] bytes;
//string ContentType;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select SaleFileName from Sales";
cmd.Parameters.AddWithValue("@SaleFileName", SaleFileName );
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["SaleFileName"];
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
// Read the original file from disk
FileStream myFileStream = new FileStream( filePath ,FileMode.Open);
long FileSize = myFileStream.Length;
byte[] Buffer = new byte[Convert.ToInt32(FileSize)];
myFileStream.Read(Buffer, 0, Convert.ToInt32(FileSize));
myFileStream.Close();
// // Tell the browse stuff about the file
Response.AddHeader("Content-Length", bytes.ToString());
//Response.AddHeader("Content-Disposition", "inline; filename=" & fileName.Replace(" ", "_"));
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
Response.TransmitFile(fileName);
Response.ContentType = "application/octet-stream";
// Send the data to the browser
Response.BinaryWrite(Buffer);
Response.End();
}
}