views:

494

answers:

4

I'd like to respond to an http request with both a txt file and an html page. This way the client can save the file and see a summary of that file's contents via an html page.

Since the file is generated on the fly, I have to use state management on the server to generate the summary on the second request. I'd like to avoid this and wrap this up in a single response.

What would the custom ActionResult look like?

A: 

Why not render the summary as normal HTML View and provide a link under that summary that points to a separate ActionResult that returns binary data...

public ActionResult SendFile(int id)
    {
        File file = FileRepository.GetFile(id);
        byte[] fileData = file.FileData.ToArray();
        return File(fileData, file.FileMimeType, file.FileName);
    }
mikerennick
I'm doing already doing this. This method forces me to do some state tracking on the server since the second action wouldn't have the data that was used to render the first html form. I'm seeing if there is a way to do this without dealing with the session (so thats including tempdata).
TheDeeno
The data is generated on the fly - I could save it to a db also, but that's just another form of state tracking. It should be throw away.
TheDeeno
A: 

You could send XML with reference to an XSLT stylesheet. The browser can do the transformation and display a resulting HTML output.

And the user may simple do "File->Save as ..." to store the XML file.

mkoeller
Thanks for the brain storm. Unfortunately, my file isn't xml and can't be. Its format is determined by a legacy accounting system. Thoughts on the multi-part response?
TheDeeno
What is the typical size of your response data?
mkoeller
A: 

I did some search on this topic and to my surprise, this seems people did this already. I found a description here, but it does not provide a public test URI, so I couldn't test browser behaviour on this. It also mentiones the solution to send an archive file to the client. But I assume that's not what you're looking for, right?

Here is another article showing an example with multipart responses. Sadly, the demo URI seems not to be working any more.

So the situation is this: HTTP multipart responses can be done. There are client (few) libraries to handle those in application code. Browser behaviour is to be tested (Here's a voice). Using this feature seems experimental and may be recommended only you can control both ends of the communication to eliminate interoperability issues.

mkoeller
+3  A: 

This was also discussed here on SO.

i answered it like this:

Nope, multipart attachments for download (like as in email) aren't supported for security reasons. It's called a "drive-by download."

Note that Gmail handles this by dynamically zipping up the files. You should too. http://forums.asp.net/t/1240811.aspx

I don't believe any modern browser supports multipart/mixed.

Scott Hanselman