c# - Need current file download option from the Gridview when file is uploaded -


i have gridview upload file , save server. there in it, if wrong file uploaded can again upload same file , updated version been reflected on gridview. here code:-

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 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 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;         }     } 

for checking if same file exits or not

public int checkfileexists(string filename)     {         try         {             using (sqlconnection con = new sqlconnection(system.configuration.configurationmanager.connectionstrings["defaultcsrconnection"].connectionstring))             {                 sqlcommand cmd = new sqlcommand("select count(*) tbl_reports 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;         }     } 

now want is,

what in scenario if want download current file gridview.

please suggest.

updated gridview code:-

<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 sure want delete record?')" />                         </itemtemplate>                     </asp:templatefield>                 </columns></asp:gridview> 

follow steps here

  1. add templatefield @ end below.
    <asp:templatefield>     <itemtemplate>      <asp:linkbutton id="lnkdownload" text="download" commandargument='<%# eval("report_file") %>' runat="server" onclick="downloadfile"></asp:linkbutton>       </itemtemplate>     </asp:templatefield> 

2) c# code:-

protected void downloadfile(object sender, eventargs e)     {         try         {             string filepath = (sender 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;         }     } 

Comments

Popular posts from this blog

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

java - Incorrect order of records in M-M relationship in hibernate -