c# - How to make the download image link work? -


i have code viewdepositslip.aspx wherein shows imageurls folder uploaded files stored:

<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" emptydatatext = "no files uploaded"> <columns>     <asp:boundfield datafield="text" headertext="file name" />     <asp:templatefield>         <itemtemplate>             <asp:linkbutton id="lnkdownload" text = "download" commandargument = '<%# eval("value") %>' runat="server" onclick = "downloadfile"></asp:linkbutton>         </itemtemplate>     </asp:templatefield>     <asp:templatefield>         <itemtemplate>             <asp:linkbutton id = "lnkdelete" text = "delete" commandargument = '<%# eval("value") %>' runat = "server" onclick = "deletefile" />         </itemtemplate>     </asp:templatefield> </columns> 

this code uploading images folder(this client side, different webform).

    protected void uploadfile(object sender, eventargs e)     {     string filename = path.getfilename(fileupload1.postedfile.filename);     fileupload1.postedfile.saveas(server.mappath("~/uploads/") + filename);     response.redirect(request.url.absoluteuri); } 

and code behind. delete button functions cannot make download link function work. missing here?

 protected void page_load(object sender, eventargs e)     {         if (!ispostback)         {             string[] filepaths = directory.getfiles(server.mappath("~/bankdeposituploads/"));             list<listitem> files = new list<listitem>();             foreach (string filepath in filepaths)             {                 files.add(new listitem(path.getfilename(filepath), filepath));             }             gridview1.datasource = files;             gridview1.databind();         } protected void downloadfile(object sender, eventargs e)     {         string filepath = (sender linkbutton).commandargument;         response.contenttype = contenttype;         response.appendheader("content-disposition", "attachment; filename=" + path.getfilename(filepath));         response.writefile(filepath);         response.end();     }     protected void deletefile(object sender, eventargs e)     {         string filepath = (sender linkbutton).commandargument;         file.delete(filepath);         response.redirect(request.url.absoluteuri);     } 

also i'm planning see actual image once shown in gridview. of has image urls.

your code should work reason might not working try way:

aspx page

 <asp:linkbutton id="lnkdownload" runat="server" commandname="cmd">download</asp:linkbutton> 

cs page

   protected void gridview1_rowcommand(object sender, system.web.ui.webcontrols.gridviewcommandeventargs e)         {             if (e.commandname == "cmd")             {                 string filepath = (sender linkbutton).commandargument;                 response.contenttype = contenttype;                 response.appendheader("content-disposition", "attachment; filename=" + path.getfilename(filepath));                 response.writefile(filepath);                 response.end();             }     } 

edit:- since using update panel have postback when clicking on link button. there many ways doing this. explaining 2 ways. put code @ page load

scriptmanager.getcurrent(this).registerpostbackcontrol(this.gridview1); 

or use trigger in aspx page, below </contenttemplate>

<triggers>         <asp:postbacktrigger controlid="gridview1" /> </triggers> 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -