Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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);
    }

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top