我在ASP.NET项目中使用上载和下载方法的下载链接。我用字段
创建了一个数据库 tFileName (nvarchar 100) , tFileupload (nvarchar 100), tFileData (varbinary (max))
。
当我上传数据并保存字段时,数据会保存。我的问题是我似乎无法从数据库下载数据。
生病了我的代码和我的c#代码。希望可以有人帮帮我。谢谢!
我的asp.net代码,此代码也位于我的UpdatePanel
内。
<asp:GridView ID="gvFile" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" CellPadding="5">
<Columns>
<asp:BoundField DataField="tFileName" HeaderText="File Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click" CommandArgument='<%# Eval("TravelNumber") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我的aspx.cs
public void getFile()
{
Utility u = new Utility();
string conn = u.local();
using (SqlConnection connUser = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select TravelNumber, tFileName from TravelTransaction";
cmd.Connection = connUser;
connUser.Open();
gvFile.DataSource = cmd.ExecuteReader();
gvFile.DataBind();
connUser.Close();
}
}
}
protected void lnkDownload_Click(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
Utility u = new Utility();
string conn = u.local();
using (SqlConnection connUser = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select tFileName, tFileUpload, tFileData from TravelTransaction where TravelNumber = @Trav";
cmd.Parameters.Add(new SqlParameter("@Trav", id));
cmd.Connection = connUser;
connUser.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["tFileData"];
contentType = sdr["tFileUpload"].ToString();
fileName = sdr["tFileName"].ToString();
}
connUser.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("content-disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
我似乎无法找到我的错误,实际上它没有错误。但是文件没有下载,也没有动作。我的代码出了什么问题?