5tar-Kaster
5tar-Kaster

Reputation: 908

adding download to an anchor tag using C#

So basically the html looks like this

<a href='test.pdf' Download>download test</a>

But I need this to be made in C# what I have so far is

HtmlAnchor link = new HtmlAnchor();
link.Href = "test.pdf";
link.innerText = "download test";

How do I put that "Download" part in so that when you click the link it would actually download the file and not link to it?

Upvotes: 1

Views: 3264

Answers (2)

Priya Gund
Priya Gund

Reputation: 156

Try this one: Place in your html page, in C#, Write: litdoc.Text += "" + "download test" + ""; In handler: mention code to download pdf file, just like :

    string file = "";
    file = context.Request.QueryString["file"]; 
    if (file != "")
    {
        context.Response.Clear(); 
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("content-disposition", "attachment;filename="    +           Path.GetFileName(file));
        context.Response.WriteFile(file);
        context.Response.End();

    }

where path is the location from where you download pdf file.

Upvotes: 1

Adil
Adil

Reputation: 148180

You need to use InnerHtml instead of InnerText along with <b> for bold

link.InnerHtml = @"<b>download test</b>";

Edit based on OP Edit,

You will need to use Response.WriteFile on linkButton click event, you probably look for something being asked in this post.

FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
Response.WriteFile(fileInfo.FullName);
Response.End();

Upvotes: 0

Related Questions