How to return data to the client as though it were a file?

P

polaris431

In my ASP.NET app, I would like to return data back to the client in
such a way that it appears to the client that a file is being returned.
As you know, when you click on a link on some web pages that have a
hyperlink to a file such as a zip file, a dialog pops up in the
client's browser asking where they want to save the file. In a similar
way, I would like to accomplish the same thing, the only difference
being that the data I send back to the client doesn't come from a file
but is somehow fed back through some mechanism. I could write a
temporary file and let them download it but then I would need to come
up with another mechanism to delete the file after a period of time
(hours or days) since the file really was only meant to be temporary
and I don't want temporary files accumulating on my server. The web app
is such that the client posts a file, the app reads the content of the
file (without it getting stored on the server), performs some
calculations on the data, and then at this point should return the
calculations. I want the calculatoins however to get stored on the
client's side as a file.
 
C

Chris Fulstow

You can do this by:
1. Setting the appropriate MIME type
2. Adding a content-disposition header to specify the filename
3. Writing data to browser

For example:

Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("content-disposition",
"attachment;filename=myfile.zip");
Response.WriteFile(filename);
Response.End();
 
P

polaris431

I actually found the solution posted in the newsgroup but their
solution left out the Response.End() that your code shows. This was
critical because without it, the web page HTML also ends up getting
stored in the file on the client's side if the Response.End is left
out. The HTML ends up being appended to the data I really want saved.

Thanks for your support.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top