takirala
takirala

Reputation: 2019

asp.net website file download history

I have a website developed in ASP.NET that contains links to 50 different PDF files. Website has authentication functionality too. Now when a user downloads a particular file, i need to populate a database with the user name and file name. Tip me on how to achieve this. PS: i have tried using sessionvariable and onclickevent for pdf icon, but it is too messy for >50 pdf files.

Upvotes: 1

Views: 457

Answers (3)

Chase Florell
Chase Florell

Reputation: 47417

You'll have to create a download handler. you can loop through physical files on the server, or code them into some sort of database.

Inside that handler, you'll want to have some method to record the "click" function.
note: this will not account for cancelled downloads.

markup

<asp:HyperLink id="hyperlink1" ... />

code behind

hyperlink1.NavigateUrl = "Downloader.ashx?file="+"filename";

handler

<%@ WebHandler Language="C#" Class="Downloader" %>

using System;
using System.Web;

public class Downloader : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        // create a file response
        HttpResponse r = context.Response;
        r.ContentType = // set the appropriate content type;

        string file = context.Request.QueryString["file"];

        // *****
        // LOG THE REQUEST
        // *****

        // write out the file
        r.WriteFile(...);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

Upvotes: 2

Daniel Powell
Daniel Powell

Reputation: 8303

You could create a http handler that would be used for requesting all documents.

For instance the link you would put on the page would be http://yoursite/pdf?name=MyFancyPDf

In the handler you can then go and do whatever processing is neccessary for the pdf (including logging) and then serve it up to the user

Upvotes: 1

Mark Redman
Mark Redman

Reputation: 24535

I would create an httpHandler to get files for download, this will enable you to abstract the direct links to files, giving to control to secure specific files and save who clicked on files to your database and enable you to store files anywhere in the file system, ie not make these direct links for added security)

Upvotes: 0

Related Questions