我有一个gridview,我上传文件并将其保存到服务器。还有一些额外的东西,比如如果上传了错误的文件,我们可以再次上传相同的文件,并且其更新版本已反映在gridview 上。这是我的代码: -
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
using (SqlCommand cmd = conn.CreateCommand())
{
if (fupreportfile.HasFiles)
{
int count = CheckFileExists(fupreportfile.PostedFile.FileName);
fupreportfile.SaveAs(Server.MapPath("~/ReportFolder/" + fupreportfile.PostedFile.FileName));
if (count > 0)
{
cmd.CommandText = " Update tbl_reports SET revision=@revision Where Id=@Id";
cmd.Parameters.AddWithValue("@Id", GetIdByFileName(fupreportfile.PostedFile.FileName));
cmd.Parameters.Add("@revision", SqlDbType.VarChar).Value = (count + 1).ToString();
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Reports updated sucessfully');window.location ='csrreports.aspx';", true);
}
else
{
conn.Open();
SqlCommand cmd1 = new SqlCommand("Insert into tbl_reports (NgoId,report_type_id,report_title,report_file,report_desc,revision) values(@NgoId, @report_type_id, @report_title,@report_file,@report_desc,@revision)", conn);
cmd1.Parameters.Add("@NgoId", SqlDbType.Int).Value = ddlNgoName.SelectedValue;
cmd1.Parameters.Add("@report_type_id", SqlDbType.Int).Value = ddlReportType.SelectedValue;
cmd1.Parameters.Add("@report_title", SqlDbType.NVarChar).Value = txtreporttitle.Text;
cmd1.Parameters.Add("@report_file", SqlDbType.VarChar).Value = fupreportfile.PostedFile.FileName;
cmd1.Parameters.Add("@report_desc", SqlDbType.NVarChar).Value = txtreportdescription.Text;
cmd1.Parameters.Add("@revision", SqlDbType.VarChar).Value = (count + 1).ToString();
cmd1.ExecuteNonQuery();
conn.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Reports added sucessfully');window.location ='csrreports.aspx';", true);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
用于检查同一文件是否退出
public int CheckFileExists(string fileName)
{
try
{
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM tbl_reports WHERE report_file=@report_file", con);
cmd.Parameters.Add("@report_file", SqlDbType.VarChar).Value = fileName;
con.Open();
int count = (int)cmd.ExecuteScalar();
return count;
}
}
catch (Exception ex)
{
throw ex;
}
}
现在我想要的是,
如果我想从gridview下载当前文件,该怎么办。
请建议。
更新的GRIDVIEW代码: -
<asp:GridView ID="grdReports"
runat="server"
Width="100%" border="1"
Style="border: 1px solid #E5E5E5;"
CellPadding="3"
OnPreRender="PreRenderGrid"
AutoGenerateColumns="False"
AllowPaging="true"
CssClass="hoverTable"
DataKeyNames="Id"
EmptyDataText="No Records Found!"
HeaderStyle-CssClass="k-grid td"
OnRowCommand="grdReports_RowCommand"
OnDataBound="grdReports_DataBound"
PageSize="10"
ShowFooter="false"
OnPageIndexChanging="grdReports_PageIndexChanging"
OnRowDeleting="grdReports_RowDeleting">
<AlternatingRowStyle CssClass="k-alt" />
<Columns>
<asp:TemplateField HeaderText="Select" ItemStyle-Width="5">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" onClick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField DataField="title" HeaderText="Report Type" ItemStyle-Width="30" />--%>
<asp:BoundField DataField="report_title" HeaderText="Report Title" ItemStyle-Width="30" />
<asp:BoundField DataField="report_file" HeaderText="Report File" ItemStyle-Width="30" />
<asp:BoundField DataField="report_desc" HeaderText="Report Description" HtmlEncode="false" ItemStyle-Width="30" />
<asp:BoundField DataField="revision" HeaderText="Report Revision" ItemStyle-Width="30" />
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%" HeaderStyle-CssClass="k-grid td">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" AlternateText="Edit" ImageUrl="~/images/edit.png" runat="server" Width="15" Height="15" CommandName="eEdit" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" />
<asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
答案 0 :(得分:1)
按照此处的步骤
TemplateField
,如下所示。 <asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("report_file") %>' runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
2)C#代码: -
protected void DownloadFile(object sender, EventArgs e)
{
try
{
string filePath = (sender as LinkButton).CommandArgument;
System.Net.WebClient req = new System.Net.WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath("~/ReportFolder/" + filePath) + "\"");
byte[] data = req.DownloadData(Server.MapPath("~/ReportFolder/" + filePath));
response.BinaryWrite(data);
response.End();
}
catch (Exception ex)
{
throw ex;
}
}