473,408 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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.

Oct 13 '06 #1
2 1081
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();

polaris...@gmail.com wrote:
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.
Oct 13 '06 #2
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.
On Oct 13, 8:39 pm, "Chris Fulstow" <chrisfuls...@hotmail.comwrote:
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();

polaris...@gmail.com wrote:
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.- Hide quoted text -- Show quoted text -
Oct 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
34
by: priyanka | last post by:
Hi, I was wondering if we could parse or do something in the executable( whose source language was C). How can I use some scripting language like perl/python to find out the information about...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.