5tar-Kaster
5tar-Kaster

Reputation: 908

How to do dynamic downloads links

Its probably not the correct title for asking this question as when I searched it gave results of dynamic urls for downloads. Thats not what I want... (at least I think it isn't)

My issue is that I need to dynamically create a list of, say newsletters as an example, the list gets created using loops in C# code and Response.Write(). The source is from a database that just contains the location source of the newsletter, date and name. Basic output of the code is:

|News Letters
|Date:       |Name:        |View button: |Download link:  |
|7/01/2013   |June News    |   [View]    |   [Download]   |
|8/01/2013   |July News    |   [View]    |   [Download]   |

more or less... The table is generated in a loop, so it can be any length.

How would I go about implementing a download for each individual news letter? The HTML code is just using an ordinary table and the view button is an href link to the souce.

Upvotes: 1

Views: 1312

Answers (2)

Julie20
Julie20

Reputation: 228

If you're using HTML5 you can just add the word 'download' to the end of the href link.

<a href="link to newsletter.pdf" download>Download link</a>

this will force a download instead of opening the pdf like normal.

Good luck!

Upvotes: 2

rh072005
rh072005

Reputation: 740

I'm not 100% on how you're trying to return the information but this will take a SqlDataReader and add each row in the data reader response to a new row in a table and build up a HTML string.

StringBuilder sb = new StringBuilder();
        sb.Append("<table><th>Date</th><th>Name</th><th>View</th><th>Download</th>");


        while (sqlDataReader.Read())
        {
            sb.Append(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td><a href='{3}' download>Download</a></td></tr>", sqlDataReader["Date"], sqlDataReader["Name"],
                          sqlDataReader["LocationSource"], sqlDataReader["LocationSource"]));
        }

        sb.Append("</table>");

        return sb.ToString();

I've made a slight adjustment, noticed I've put download inside the link tag. This is a HTML 5 feature that will force a download rather than opening in browser, more info here Force a browser to save file as after clicking link

Upvotes: 4

Related Questions