0

I have a web page used to display the information about files (like a name and a pic ) of the file based on the (query string) ((the files already will be saved on the server))

the details of these files will display within (data list).. how to creat a command button to download the file when click on it

void FillLessons() { SqlConnection cn = new SqlConnection(cs); cn.Open(); SqlCommand cmd = new SqlCommand(); string sqlStatment = "SELECT Lesson_Id,Lesson_Title,Subject_Id,Lesson_Img FROM [Ali].[dbo].[tblLessons] where Subject_Id ='" + Request.QueryString["id"].ToString() + "' "; cmd.CommandType = CommandType.Text; cmd.CommandText = sqlStatment; cmd.Connection = cn; DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); dllessons.DataSource = dt; dllessons.DataBind(); } 

I dont know how to extract the file name (Lesson_Title) from the data table (dt) to use it when I want to create a button to download the file??

I have added a link button inside the data list and added command name=Download

and CommandArgument='<%# Eval("Lesson_Title") %>'

than typed this code

 protected void dllessons_ItemCommand(object sender, DataListCommandEventArgs e) { if (e.CommandName == "Download") { Response.Clear(); Response.ContentType = "application/octect-stream"; Response.AppendHeader("content-disposistion", "filename=" + e.CommandArgument); Response.TransmitFile (Server.MapPath("~/My_Lessons/") + e.CommandArgument); Response.End(); } } 

but it still does not work??

thanks

2
  • 1
    You should do a search on reading rows and columns from DataTables. This will allow you to get a string with the right filenames. Commented Mar 22, 2016 at 11:34
  • what framework are you using ? Commented Mar 22, 2016 at 11:46

2 Answers 2

1

ASPX;

 <asp:TemplateField> <ItemTemplate> <asp:Button ID="downloadButton" runat="server" CommandName="DownloadFile" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Download File" /> </ItemTemplate> </asp:TemplateField> 

Code behind;

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "DownloadFile") { int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[index]; //Label or HiddenField for getting file url Label lblFileUrl = (Label)row.FindControl("YourFileUrlControlId") } } 
Sign up to request clarification or add additional context in comments.

1 Comment

this is jusat a sample for grid. Just search on google; "How to use datalist with command button in ASP.NET"
0

This might be beyond the scope of what you are looking for but I normaly do this with script.

Create an iframe:

downloadFile: function (versionId) { var id = versionId; var hiddenIFrameID = 'hiddenDownloader', iframe = document.getElementById(hiddenIFrameID); if (iframe === null) { iframe = document.createElement('iframe'); iframe.id = hiddenIFrameID; iframe.style.display = 'none'; document.body.appendChild(iframe); } iframe.src = "/File/DownloadFile/?fileId=" + id; }, 

Server code:

 public ActionResult DownloadFile(Guid fileId) { var file = _FileService.GetFileData(fileId); return File(file.Stream, file.ContentType, file.Name); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.