Web Forms :: How To Write Response.OutputStream To A File Preferably In C#
Feb 4, 2010How do I write Response.OutputStream to a file preferably in C#?
View 2 RepliesHow do I write Response.OutputStream to a file preferably in C#?
View 2 RepliesThe application is hosted in Windows Server 2008 with IIS7.
I have a button that calls a new webpage (asp.net page, ext .aspx) that contains
Response.OutputStream.Write(blahblahblah) which then should prompt a download box to allow user to save an image.
Now the page basically creates a stream and then is supposed to output the stream to the user using Response.Outputstream.Write.
Then the user is supposed to be prompted to open or save the document. This works fine in Firefox but it does not work with IE7 or 8
Also this exact same code works with IIS6 on a Windows 2003 server.
So the only differences is the IIS6 and Windows 2003 Server to IIS7 and Windows 2008 Server.
And what happens is when I clicked the button, the page did pop up but disappear 1-2 seconds later without
prompting the download box to the user.
Here is the code that launches for Reference
//e.g file = 123424_43535.jpeg
FileInfo myFile = new FileInfo(Server.MapPath("mapImage/") + file;
using(var fs = myFile.Open(FileMode.Open, FileAccess.Read))
{
byte[] buffer;
int read;
buffer = new byte[(int)fs.Length];
Response.AddHeader("Content-Disposition", "attachment;filename=" + file);
Response.ContentType = "application/x-force-download";
Response.AddHeader("Connection", "Keep-Alive");
Response.AddHeader("Content-Length", fs.Length.ToString());
while((read = fs.Read(buffer, 0, buffer.Length)) > 0)
{
this.Response.OutputStream.Write(buffer, 0, read);
}
}
Response.End();
I have tried to replace Response.AddHeader with Response.AppendHeader, however the result is still the same. Also, I have tried to replace the Response.ContentType with application/octet-stream and image/jpeg, I also faced the same result.
Is there any setting/modification on server/code that I need to look on?
I'm using the technic described in: [URL]
to allow users to download files. Our group uses a tool called SecureGateway which doesn't seem to like FTP as a protocol.
Smaller files don't seem to have a problem, but intermittently larger files (larger than 10K) will not download to the client properly. They will just abruptly end the transfer as if they are actually completed.
I use the following code:
{
System.IO.
Stream iStream =
null;// Buffer to read 10K bytes in chunk:
byte[] buffer =
new
Byte[10000];// Length of the file:
int length;// Total bytes to read:
// string filepath = "DownloadFileName";
long dataToRead;// Identify the file to download including its path.
String filepath = Request.Params["file"].ToString();//
Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,FileAccess.Read,
System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = Response.AddHeader("application/octet-stream";"Content-Disposition",
"attachment; filename="" + filename +
""");
// Read the bytes.
while (dataToRead > 0)
{ // Verify that the client is connected.
if (Response.IsClientConnected)// Read the data in buffer.
{
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer = new
Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
{
catch (Exception ex)// Trap the error, if any.
{
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream !=
null)//Close the file.
iStream.Close();
}
}
}
I know that there is a preset size restriction that you have to override when uploading a file to the server, just wondering if someone knows of a similar issue with downloading to the client.
I am a newbie of asp.net.
The environment is .net 3.5 and C#.
There are files stored in sql server (datatype of column is varbinary in the database).
each record is in one record in that table (FILE_TABLE).
And there is another table (PATH_TABLE) to store the files paths.
for example,
when the user download the folder A (select an item on the tree and click a button on the client side),
there is one sub-folder B and two files A-1 and A-2.
in folder B, there is two files B-1 and B-2.
then, at the code behind (server), the server will get the files from the database and convert them (including the files and subfolders) to a .zip file (or other compressed files)
and then write a response (using the method HttpResponse.BinaryWrite(Byte[] xxx) )
to client side and let the user download.
after the download, the user can extract the compressed file and the structure of the folders and files keep unchanged.
have the following
Response.ContentType =
"application/octet-stream";
Response.AppendHeader("Content-Transfer-Encoding",
[code]...
Error 1 'Response' is an ambiguous reference between 2 references that have a Response.Neither is the one I need,how do i reference the Response that write the file?
I've recently developed a web user control that lists a series of reports. When the user clicks on the report it serves back a CSV file download in the response stream using the following code:
Response.Clear();
Response.ContentType = "text/CSV";
Response.CacheControl = "no-cache"; [code].....
The code initially worked fine in all browsers. Then the client put a requirement to use SSL for the site. As part of this I introduced a global handler to update the protocol from HTTP to HTTPS for all requests as follows:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string requestURL = Request.Url.ToString().ToLower();[code]....
However, since securing the site using SSL the CSV file downloads no longer work for IE although they do continue to work for Firefox / Chrome / Safari.
Is there something I am missing in the headers that is unique to IE in order for the file response to work correctly?
The message I receive from IE is:
"Internet Explorer cannot download
Reports.aspx from .... in ......
Internet Explorer was unable to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later."
UPDATE:
Here is some example fiddler output coming back from the page request which looks like it's serving correctly. Why doesn't IE understand that it's just been served a file?
HTTP/1.1 200 OK
Date: Tue, 09 Nov 2010 14:23:50 GMT
Server: Microsoft-IIS/6.0[code].....
I am a newbie of handling memory.
in my web apps, files are stored as varbinary in sql server.
my goal is to make a response to let users to download the files.
if there is a file (500 MB) stored in one record in the sql server,
what is the best way to let users download it?
I found that , in code-behind, if the files are loaded as byte[] objects, it uses the memory.
for example, the datalength of a file in the database is 500 MB.
the byte[] for this file uses 500 MB memory. am I correct?
how about the filestream? if the file is loaded as a filestream object in code-behind.s
does it also take 500 MB memory?
what is difference between Response.write and Response.output.write
View 2 RepliesI have an httpmodule and it has a handler for OnEndRequest. I am trying to write the HttpContext.Response.OutputStream to a file. I am trying to use the Read method of it. But when it is called the exception message i get is "Specified method is not supported". So i am not able to do what i want that is, write the response to a file on the disk. I am able to write the HttpContext.Request.InputStream to a file using its Read method which i do in a handler for OnBeginRequest.
View 2 RepliesI'm trying to create a ZIP file on the fly which might contain a few thousands of pictures.
[code]....
My question:
Is there a way to initiate the download (let the download manager on client side popup), and then start writing on the stream?
I monitored w3wp.exe (IIS) process, and it seems that the data is being written on memory instead of Stream.
When w3wp.exe memory usage riches a certain number, it releases the memory and nothing happens (no download).
I am trying to change the text of a asp:textbox and collapse some ajaxToolkit:CollapsiblePanelExtenders within some ascx controls on my page as well as output a dynamically generated file. I have no problem collapsing the CollapsiblePanelExtenders and changing the text of the textbox from the codebehind or outputting a file. The problem arises when I want BOTH of these events to happen on the same postback. Unfortunately using Response.Write negates all of the other changes to the page.
View 1 RepliesI need to to export a file to the user. It takes 1-2 min to generate the file so I'd like to have the page go into a kind-of modal mode with a layover on the page and a 'Working' spinner showing. The problem is I can't make the modal stuff go away after Save File dialog is closed.
How can I remove the layover after the dialog is done?
i am calling a function which is inside Homescroll.ascx.cs from Homescroll.ascx so i wrote on Homescroll.ascx as <% Response.Write(scroll()); %> but all this is in update panel,and i am getting errors. so is their any other way to call function from homescroll.ascx to homescroll.ascx.cs,instead of response.write();
View 3 Replieshow can i get get below value into asp:textbox
Response.Write("<script id=""mycountry3"" language=""Javascript"" src=""http://www.ip2phrase.com/ip2phrase.asp?template=You are from Country: <COUNTRY> ""></script>")
in a webpage I have a link to let the user download file, such as,
<a href="showfile.aspx?filename=xxx">download file</a>
in showfile.aspx, I send the file using Response.OutputStream.Write method. now I get some problem when somebody put this webpage in an IFrame and open in IE, as I checked the code, showfile.aspx is requested twice when clicks the link, and in the second time the cookies of authorization and session Id are missing. I tried to add the p3p header but not working.my question is, is this how the IE designed with iframe? is there anyway to work around?
I have an aspx page and when the user clicks a button the following code runs:
Response.Write(string.Format("<script language='javascript'> window.open('DisplayImage.aspx?DocumentID={0}', 'window','HEIGHT=600,WIDTH=820,top=50,left=50,toolbar=yes,scrollbars=yes,resizable=yes');</script>",
id));
The variable id is declared and it's value set eralier in the method.When the new window opens it displays the image properly, but the existing page suddenly loses all of it's styling. The links double in size and change font family. Does anyone know a way to retain styling on the calling page?
I'm doing something for paypal.
So i have my values for paypal PDT like this:
[Code]....
and then i will send the form to paypal and get the values (i also need some javascript to do that but it's irelevant now).
[Code]....
So it works ok but it show me all the paypal values on the page.Logical since i do a response.write.
Now my question is if there is a way that i will not get to see the values on the page.
I've tried request.clear ,request.flush but i didn't come up with anything.
I want to use response.write to generate a table. but I have a question, the result of table must display on the top of web page. How can I control the location of table? e.g. at the middle or end of web page or starting from number of line.
View 5 Replieshow to write two files using Response.WriteFile?
I tried the following
Response.AddHeader("Content-Disposition", "attachment; filename=" + textFile);
Response.Flush();
Response.WriteFile(FilePath);
Response.End();
streamWriter2.WriteLine(CodeFileText);
streamWriter2.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + textFile2);
Response.Flush();
Response.WriteFile(FilePath2);
Response.End();
But it downloads the first and stops.
It doesnot go for second one.
I'm trying to pass a calculation from a response.write to a label.
[Code]....
I want to pass (ts.minutes) to durationLabel
i am posting some data from my application to an external website i.e for a credit card payment
so i am using a response.write object.how to display the reposnse.write in a new window
this is my code
Dim sb As New StringBuilder()
sb.AppendFormat("runat=server")
sb.Append("<html>")
sb.AppendFormat("<body onload='document.forms[""form""].submit()'>")
sb.AppendFormat("<form name='form' action='{0}' method='post'>", postbackUrl)
[Code]....
I have written simple HttpModule. context.Response.Output.Write is working fine. but not context.Response.Write().
View 2 Repliesthe response.write() output does not show in content placeholder but on top of the page.
[Code]....
I tried adding response.write dateTime.toLongTimeString in the page load and prerender but that is causing problems
View 6 RepliesIs there a simple way to use Response.Write to adjust column widths when exporting data to MS Excel?
View 3 Replies