1

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:

  • Superfiualy related to this question except that I must generate a temporary file.
Community
  • 1
  • 1
BCS
  • 75,627
  • 68
  • 187
  • 294

3 Answers3

2

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

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"/>
BCS
  • 75,627
  • 68
  • 187
  • 294
swilliams
  • 48,060
  • 27
  • 100
  • 130
1

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()
Shea Daniels
  • 3,232
  • 3
  • 23
  • 27
  • What exactly is the requirement for the `Response.End()`? Is it needed or does ASP handle that for you if you just fall out the end of the function? – BCS Aug 19 '09 at 16:37
  • I'm not sure that there is one. It might depend on where your code is located in your project. But if you put the Response.End() in there you know for sure that nothing else will be sent back to the requester. – Shea Daniels Aug 19 '09 at 21:48