Link to file (for Download it)..

Last post 07-06-2009 1:14 AM by sirdneo. 13 replies.

Sort Posts:

  • Link to file (for Download it)..

    07-03-2009, 9:11 PM
    • Member
      134 point Member
    • b007
    • Member since 06-21-2008, 8:27 PM
    • Posts 1,112

    Hi,

    How to put link to file for download  it?

    Which control to use? and what to Write (How to ..) the Path of the file

     

    Thanks..

  • Re: Link to file (for Download it)..

    07-03-2009, 10:18 PM
    • Member
      437 point Member
    • jewelhere
    • Member since 01-18-2009, 6:55 AM
    • Dhaka, Bangladesh
    • Posts 110

    in asp.net:

    <asp:LinkButton id="hj1" Text="Download" runat="server" OnClick="hj1_Click" ForeColor="blue" ToolTip="Click here to download file"    ></asp:LinkButton>

    in c#:

    private void DownloadFile(string fname, bool forceDownload)
        {
            //string path = MapPath(fname);
            //string path = fname; System.IO.Path.Combine(File Path, "jewel.xls");
            string name = System.IO.Path.GetFileName(fname);
            string ext = System.IO.Path.GetExtension(fname);
            string type = "";
            // set known types based on file extension  
            if (ext != null)
            {
                switch (ext.ToLower())
                {
                    case ".htm":
                    case ".html":
                        type = "text/HTML";
                        break;

                    case ".txt":
                        type = "text/plain";
                        break;

                    case ".doc":
                    case ".rtf":
                        type = "Application/msword";
                        break;
                }
            }
            if (forceDownload)
            {
                Response.AppendHeader("content-disposition", "attachment; filename=" + name);
            }
            if (type != "")
                Response.ContentType = type;
            Response.WriteFile(fname);
            Response.End();
        }

        protected void hj1_Click(object sender, EventArgs e)
        {
            string fileName = "";
            fileName = System.IO.Path.Combine(File Path, fName);   //file path like "C://jewel.xls"
            DownloadFile(fileName,true);
        }

    try in this way not exactly cause there's may b some spelling error but the logic is right

    Please mark as answer if this helps u.


  • Re: Link to file (for Download it)..

    07-03-2009, 10:58 PM
    Answer
    • Member
      650 point Member
    • Bobby-Z
    • Member since 10-04-2008, 5:53 AM
    • Orange County, CA
    • Posts 297

     A simple way would be if you have a video file, or zip or pic I usually just us an

    <asp:HyperLink runat="server" Text ="Download File &gt;" NavigateUrl="~/DownloadFiles/file.zip" />

    the NavigateUrl = Whatever directory you have the file in ~ = root folder

     

    "Success is the Sum of Small Efforts, Repeated Day in and Day Out - Without Ceasing!"

    Robert Hall
    CEO and Founder
    My Service Solutions, Inc.
  • Re: Link to file (for Download it)..

    07-04-2009, 3:41 AM
    • Star
      11,849 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,328

    <a href="download.pdf">download pdf</a>

  • Re: Link to file (for Download it)..

    07-04-2009, 5:48 AM
    • Member
      134 point Member
    • b007
    • Member since 06-21-2008, 8:27 PM
    • Posts 1,112

    is it will work also with EXE file?

    <asp:HyperLink runat="server" Text ="Download File &gt;" NavigateUrl="~/DownloadFiles/file.zip" />

    Thanks..

  • Re: Link to file (for Download it)..

    07-04-2009, 11:38 AM
    • Member
      134 point Member
    • b007
    • Member since 06-21-2008, 8:27 PM
    • Posts 1,112

     Plz tell me what to do?

  • Re: Link to file (for Download it)..

    07-05-2009, 5:05 AM
    Answer
    • Star
      12,577 point Star
    • malcolms
    • Member since 06-12-2008, 4:38 AM
    • Melbourne, Australia
    • Posts 2,062

    Yes it should work for an exe file too:

    <asp:HyperLink runat="server" Text ="Download File" NavigateUrl="~/DownloadFiles/Program.exe" />

    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Link to file (for Download it)..

    07-05-2009, 12:14 PM
    • Member
      134 point Member
    • b007
    • Member since 06-21-2008, 8:27 PM
    • Posts 1,112

    Hi,

    I did this with HyperLink, and It's not working...

    I'm just directed to this page without downloading the file: ---> http://localhost:65032/WebSite/files/Program.exe

     

    How to fix it...?

    Thanks..

  • Re: Link to file (for Download it)..

    07-05-2009, 12:41 PM
    • Member
      52 point Member
    • ryandemelo
    • Member since 06-27-2009, 2:42 PM
    • Goa, India
    • Posts 12

    It worked exactly fine with me! Probably the default setting of your browser have something to do with this behaviour.. 

  • Re: Link to file (for Download it)..

    07-05-2009, 4:49 PM
    • Member
      134 point Member
    • b007
    • Member since 06-21-2008, 8:27 PM
    • Posts 1,112

    Is there another why of putting it for downloading? [I'll try it..]

    Thanks..

  • Re: Link to file (for Download it)..

    07-05-2009, 5:23 PM
    Answer
    • Star
      9,008 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,551

     

        Public Sub FileDownload(ByVal FilePath As String)
            Dim myFile As IO.FileInfo = New IO.FileInfo(FilePath)
            Response.Clear()
            Response.AddHeader("Content-Disposition", "attachment; filename=" & myFile.Name)
            Response.AddHeader("Content-Length", myFile.Length.ToString())
            Response.ContentType = "application/octet-stream"
            Response.WriteFile(myFile.FullName)
            Response.End()
        End Sub
    


  • Re: Link to file (for Download it)..

    07-05-2009, 5:28 PM
    • Member
      134 point Member
    • b007
    • Member since 06-21-2008, 8:27 PM
    • Posts 1,112

    Plz explain it,

    Is this What I should put on the Button to download the file?

     

     

  • Re: Link to file (for Download it)..

    07-05-2009, 6:53 PM
    • Star
      9,008 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,551

    b007:

    Plz explain it,

    Is this What I should put on the Button to download the file?

     

     

    You need the full path of a file, for example c:\downloads\download.pdf

    Then you could download that file like

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            FileDownload("c:\downloads\download.pdf")
        End Sub


     

     

     

  • Re: Link to file (for Download it)..

    07-06-2009, 1:14 AM
    Answer
    • Contributor
      7,281 point Contributor
    • sirdneo
    • Member since 12-16-2008, 12:45 AM
    • Karachi, Pakistan
    • Posts 1,147

    This should work for exe files as well if it is working for any other file type.


    make sure that in navigate url you write ~/WebSite/files/Program.exe.


    Also tell which error do you get when you click the link. If by clicking on link a page appear showing someting like Resource Not found. THen it is becase you are not using correct path. Otherwise your browser might disabled exe file downloading.

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

    My Blog
Page 1 of 1 (14 items)