BCS
BCS

Reputation: 78545

Boilerplate to serve up a generated file from ASP.NET

Does anyone know of a base class that can be used to serve up an arbitrary file (potentially dynamically generated and temporary) from ASP.NET?

The kind of interface I'm thinking of would very simple and look something like this:

class FilePage : Control // I think... 
{
    protected Filename { get; set; } // the filename to dump out
    protected bool Delete { get; set; } // Delete the (temporary) file when done?
    protected string ContentType { get; Set; } // MIME type of the file

    abstract protected void BindData();        
}

you would derive from it and, in the abstract method, create whatever file you need to, set the properties and let the base class handle the rest.

My current use case is that I want to export data as an SQLite database.


edit:

Upvotes: 1

Views: 355

Answers (3)

swilliams
swilliams

Reputation: 48910

You can create a "page" as a class that implements IHttpHandler.

public abstract class FileHandler : IHttpHandler {

    protected string Sourcename  // the filename to dump out as
    protected string Filename    // the file to dump out
    protected bool   Delete      // Delete the (temporary) file when done?
    protected string ContentType // MIME type of the file

    abstract protected void BindData();

    public bool IsReusable {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context) {

        BindData();

        context.Response.ContentType = ContentType;
        context.Response.AddHeader(
            "content-disposition", 
            "attachment; filename=" + Filename);
        context.Response.WriteFile(Sourcename);

        if(Delete) File.Delete(Sourcename);
    }
}

Then you could subclass as you said to add all the functionality you wanted. You could also add some events in there if you wanted to push handling things like that 'Delete' property.

Lastly, you would need to update the web.config to listen to the proper URLs. In the <httpHandlers> section add:

<add verb="*" path="myUrl.aspx" type="Namespace.And.Class, Library"/>

Upvotes: 2

Guffa
Guffa

Reputation: 700322

I don't know of any class like that, but you could easily write one.

You can use either an .aspx file (web form) or and .ashx file (request handler) to handle the request. How the data is returned in very similar in either case, you use the properties and methods in the HttpResponse object. In the web form you access it using the Response property of the Page object, in the request handler you get a HttpContext object as parameter to the method handling the request which has a Response property.

Set the ContentType property to the MIME type, and add a custom header "Content-Disposition" with a value like "attachment; filename=nameOfTheFile.ext".

There are several methods to write data to the response stream. You can use Response.Write to write text (which will be encoded according to the current response encoding), you can use Response.BinaryWrite to write a byte array, and you can use Response.WriteFile to write the contents of a file on the server.

As you see the data doesn't have to be in a file before writing it to the response stream. Unless you are using some tool to create the data that has to output to a file, you can create the response entirely in memory.

Upvotes: 2

Shea Daniels
Shea Daniels

Reputation: 3270

I don't know if there's a base class that can do that, but you can clear out the response headers and then write memory streams to the Response.OutputStream. If you set the content type and header correctly it should serve up the file.

Example:

Response.Clear()
Response.ClearHeaders()
Response.AddHeader("Content-Disposition", "inline;filename=temp.pdf")
Response.ContentType = "application/pdf"
stream.WriteTo(Response.OutputStream)
Response.End()

Upvotes: 1

Related Questions