roostaamir
roostaamir

Reputation: 1968

How can I force <a> links to files trigger download in the browser (and not open)

I knowt it may be a repeated question but I couldn't find a solution to my case.

Here's what I am doing:I want to create a page where admin of the site can upload files and make a description for each file,and then users can download these files.I created an admin page with a fileupload control that saves the files in downloads/files.Then I created a database with three columns: 1-downloadtitle 2-downloadmain 3-name.The admin can enter download title and download main(the description of the file) for each file.and the name column will be filled automatically with the uploaded file's name. Now here is what I did for the download page(after getting the data from the database using code behind):

<asp:Repeater ID="downloadsRepeater" runat="server">
    <ItemTemplate>
        <div class="downloadTitle"><%#Eval("downloadtitle") %></div>
        <div class="downloadMain"><%#Eval("downloadmain") %>
            <div class="downloadButtom" dir="ltr"><a href="/Downloads/Files/<%#Eval("name") %>">Download</a></div>
        </div>
    </ItemTemplate>
    <SeparatorTemplate>
        <div class="separator"></div>
    </SeparatorTemplate>
</asp:Repeater>

You can get the idea of how the page looks form the above code(I don't want the download links to redirect the user to another page.I want users to just click the download and the the file gets downloaded!) This method I used is working with doc files, but it is not working for PDF files for example!(Pdf files will be opened by the browser instead of download) So I want to know if theyre's a way of doing this the way I want it?

Upvotes: 2

Views: 2826

Answers (2)

Nimz
Nimz

Reputation: 23

Make the html hyperlink runat ="server"

Upvotes: 0

Rob
Rob

Reputation: 919

As far as I know it is not possible by just setting some special string to the href property of an <a> tag.

But you can get your desired behaviour by replacing the link by an asp.net linkbutton and a postback in which you call the following method:

public static void DownloadFile(string filename, string path)
{
    Response.ContentType = "application/unknown";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
    Response.TransmitFile(path);
    Response.End();
}

path is the full path to the file you want to send to the client and filename is the name which the file should have when it is sent (can be different from the original name).

Upvotes: 3

Related Questions