Click here to Skip to main content
15,901,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had table of "shared_files" and had a column in it "File_path", and had another column Download where i had button to download file of that specific row.
How can i retrieve URL of files from database.
I had this code.

C#
if (e.CommandName == "Download")
        {
            string URL = "";
            FileInfo fileInfo = new FileInfo(URL);

            if (fileInfo.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Flush();
                Response.WriteFile(fileInfo.FullName);
            }
        }


Now what should i that when i slick on download button of download column, the file exist in the same row get download.
help me plz.
Posted
Updated 17-Oct-11 20:41pm
v3

I have one sample for you

In Design

<asp:linkbutton id="Open" runat="server" commandargument="<%# Eval("FilePath") %>" oncommand="Download_Command" text="<%# Bind("FilePath") %>" xmlns:asp="#unknown"></asp:linkbutton>



In Code
protected void Download_Command(object sender, CommandEventArgs e)
  {
      try
      {
          string filename = (string)e.CommandArgument;

          string path = Server.MapPath(filename);
          if (File.Exists(path))
          {
              FileInfo toDownload = new FileInfo(path);
              const long ChunkSize = 10000;
              byte[] buffer = new byte[ChunkSize];

              Response.Clear();
              FileStream iStream = File.OpenRead(path);
              long dataLengthToRead = iStream.Length;
              Response.ContentType = ReturnExtension(toDownload.Extension.ToLower());
              Response.AddHeader("content-disposition", "attachment;  filename=" + filename);
              while (dataLengthToRead > 0 & Response.IsClientConnected)
              {
                  int lengthRead = iStream.Read(buffer, 0, 10000);
                  Response.OutputStream.Write(buffer, 0, lengthRead);
                  Response.Flush();
                  dataLengthToRead = dataLengthToRead - lengthRead;
              }
              Response.Close();
          }
      }
      catch (Exception ex)
      {
      }
  }


Hope be helpful,
Theingi Win.
 
Share this answer
 
Hi,

try this way you can get your requirement.

place this code in datalist of item template
ASP.NET
<asp:linkbutton id="LinkButton1" runat="server" commandargument="<%#Eval("filepath") %>" commandname="Download" xmlns:asp="#unknown">Download</asp:linkbutton>


and code behind just use what u wrote
C#
if (e.CommandName == "Download")
        {
            string URL = e.CommandArgument.Tostring();
            FileInfo fileInfo = new FileInfo(URL);
 
            if (fileInfo.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Flush();
//check your file path is correct or not
                Response.WriteFile(fileInfo.FullName);
//or   Response.WriteFile(Server.MapPath(URL));
 //if url is like ~/Downloadfiles/filename1.pdf
            }
        }


All the Best
 
Share this answer
 
Comments
Asad_Iqbal 18-Oct-11 7:13am    
I did this

<asp:DataList ID="DataList1" runat="server"
DataSourceID="FileSharingDataSource" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Vertical" Width="257px">
<alternatingitemstyle backcolor="White">
<footerstyle backcolor="#CCCC99">
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<itemstyle backcolor="#F7F7DE">
<itemtemplate>
FILE_NAME:
<asp:Label ID="FILE_NAMELabel" runat="server" Text='<%# Eval("FILE_NAME") %>' />
<br />
FILE_SIZE:
<asp:Label ID="FILE_SIZELabel" runat="server" Text='<%# Eval("FILE_SIZE") %>' />
<br />
FILE_PATH:
<asp:Label ID="FILE_PATHLabel" runat="server" Text='<%# Eval("FILE_PATH") %>' />
<br />

<asp:LinkButton ID="LinkButton1" runat="server" commandargument="<%#Eval("FILE_PATH") %>" commandname="Download" xmlns:asp="#unknown">
Download
<br />
<br />

<SelectedItemStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />


But i got error in linkButton that is "The server tag is not well formed"
Muralikrishna8811 18-Oct-11 7:22am    
commandargument='<%#Eval("FILE_PAHT")%>'

you've to give correctly first single quote(') then double qoute(")

Asad_Iqbal 18-Oct-11 7:29am    
I did this still same error

<asp:LinkButton ID="LinkButton1" runat="server" commandargument='<%#Eval("FILE_PATH") %>' commandname="Download" xmlns:asp="#unknown">
Download
Muralikrishna8811 18-Oct-11 7:31am    
check your linkbutton tag was closed or not
Muralikrishna8811 18-Oct-11 7:31am    
where you getting that error same line or ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900