Mark
Mark

Reputation: 11

How to download a file in asp.net using c#

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

Upvotes: 0

Views: 1382

Answers (2)

ThunD3eR
ThunD3eR

Reputation: 3456

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

Upvotes: 0

Mehmet
Mehmet

Reputation: 1874

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")        
      }

    }

Upvotes: 1

Related Questions