StreamWriter and File Access

G

Guest

Hi. I'm building an ASP.NET site which is hosted on a remote server. When I
try to create a new file using StreamWriter I must specify the drive location
where the file will be saved. I don't have file access anywhere on the
remote server, and therefore I can't figure out how I can create a file. Is
it possible to utilize StreamWriter to create a new file and somehow display
it for the user to save, without actually saving the file programatically?
Or is there anyway I can work around this issue?

Thanks all.
 
L

Laurent Bugnion [MVP]

Hi,
Howdy,

Yes, there is. Use combination of Response.BinaryWrite(),
Response.AddHeader(). I posted similar example here: (skip file related code,
and replace Response.WriteFile with Response.BinaryWrite(yourStream))
http://msdn.microsoft.com/newsgroup...pnet&mid=cd3319b0-8d58-47d4-b7fc-ef9943cb38e8

In case of any issues, let us know.

If it's a StreamWriter, it's probably text. I find that to write text
back directly to the Response this kind of code works well:

StreamReader input = null;
StreamWriter output = null;
try
{
// Clear the response to avoid any unwanted
// additions to the file.
Response.Clear();
output
= new StreamWriter( Response.OutputStream );

Response.ContentType = "text/ascii";

output.WriteLine( "Hello world" );
}
catch ( Exception )
{
// There was an error --> clear the response
// and return a HTML formatted error message.
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "text/html";
Response.Write( "We're sorry, there was an error." );
}
finally
{
if ( output != null )
{
// Flush the output stream to avoid losing
// something on the line...
output.Flush();
output.Close();
}
// Flush the response and close it.
Response.Flush();
Response.Close();
Response.End();
}

HTH,
Laurent
 
G

Guest

Hi Laurent,

I hope you are well. I believe he wanted to display 'save as dialog' box
automatically, instead of displaying content that user can copy from window,
that's why i went for Content-Disposition : fileattachement

Respect :)
--
Milosz


Laurent Bugnion said:
Hi,
Howdy,

Yes, there is. Use combination of Response.BinaryWrite(),
Response.AddHeader(). I posted similar example here: (skip file related code,
and replace Response.WriteFile with Response.BinaryWrite(yourStream))
http://msdn.microsoft.com/newsgroup...pnet&mid=cd3319b0-8d58-47d4-b7fc-ef9943cb38e8

In case of any issues, let us know.

If it's a StreamWriter, it's probably text. I find that to write text
back directly to the Response this kind of code works well:

StreamReader input = null;
StreamWriter output = null;
try
{
// Clear the response to avoid any unwanted
// additions to the file.
Response.Clear();
output
= new StreamWriter( Response.OutputStream );

Response.ContentType = "text/ascii";

output.WriteLine( "Hello world" );
}
catch ( Exception )
{
// There was an error --> clear the response
// and return a HTML formatted error message.
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "text/html";
Response.Write( "We're sorry, there was an error." );
}
finally
{
if ( output != null )
{
// Flush the output stream to avoid losing
// something on the line...
output.Flush();
output.Close();
}
// Flush the response and close it.
Response.Flush();
Response.Close();
Response.End();
}

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
G

Guest

Hi again,

Actually good call he could just add to your example:
Response.AddHeader("Content-Disposition", "attachment;
filename=\"test.txt\"");

Regards

--
Milosz


Milosz Skalecki said:
Hi Laurent,

I hope you are well. I believe he wanted to display 'save as dialog' box
automatically, instead of displaying content that user can copy from window,
that's why i went for Content-Disposition : fileattachement

Respect :)
--
Milosz


Laurent Bugnion said:
Hi,
Howdy,

Yes, there is. Use combination of Response.BinaryWrite(),
Response.AddHeader(). I posted similar example here: (skip file related code,
and replace Response.WriteFile with Response.BinaryWrite(yourStream))
http://msdn.microsoft.com/newsgroup...pnet&mid=cd3319b0-8d58-47d4-b7fc-ef9943cb38e8

In case of any issues, let us know.

If it's a StreamWriter, it's probably text. I find that to write text
back directly to the Response this kind of code works well:

StreamReader input = null;
StreamWriter output = null;
try
{
// Clear the response to avoid any unwanted
// additions to the file.
Response.Clear();
output
= new StreamWriter( Response.OutputStream );

Response.ContentType = "text/ascii";

output.WriteLine( "Hello world" );
}
catch ( Exception )
{
// There was an error --> clear the response
// and return a HTML formatted error message.
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "text/html";
Response.Write( "We're sorry, there was an error." );
}
finally
{
if ( output != null )
{
// Flush the output stream to avoid losing
// something on the line...
output.Flush();
output.Close();
}
// Flush the response and close it.
Response.Flush();
Response.Close();
Response.End();
}

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
G

Guest

Excellent responses...Thank you both for your great suggestions!

Milosz Skalecki said:
Hi again,

Actually good call he could just add to your example:
Response.AddHeader("Content-Disposition", "attachment;
filename=\"test.txt\"");

Regards

--
Milosz


Milosz Skalecki said:
Hi Laurent,

I hope you are well. I believe he wanted to display 'save as dialog' box
automatically, instead of displaying content that user can copy from window,
that's why i went for Content-Disposition : fileattachement

Respect :)
--
Milosz


Laurent Bugnion said:
Hi,

Milosz Skalecki [MCAD] wrote:
Howdy,

Yes, there is. Use combination of Response.BinaryWrite(),
Response.AddHeader(). I posted similar example here: (skip file related code,
and replace Response.WriteFile with Response.BinaryWrite(yourStream))
http://msdn.microsoft.com/newsgroup...pnet&mid=cd3319b0-8d58-47d4-b7fc-ef9943cb38e8

In case of any issues, let us know.

If it's a StreamWriter, it's probably text. I find that to write text
back directly to the Response this kind of code works well:

StreamReader input = null;
StreamWriter output = null;
try
{
// Clear the response to avoid any unwanted
// additions to the file.
Response.Clear();
output
= new StreamWriter( Response.OutputStream );

Response.ContentType = "text/ascii";

output.WriteLine( "Hello world" );
}
catch ( Exception )
{
// There was an error --> clear the response
// and return a HTML formatted error message.
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "text/html";
Response.Write( "We're sorry, there was an error." );
}
finally
{
if ( output != null )
{
// Flush the output stream to avoid losing
// something on the line...
output.Flush();
output.Close();
}
// Flush the response and close it.
Response.Flush();
Response.Close();
Response.End();
}

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion [MVP]

Hi,
Hi again,

Actually good call he could just add to your example:
Response.AddHeader("Content-Disposition", "attachment;
filename=\"test.txt\"");

Regards

Yes. I had that in my initial draft of the reply first, then I removed
it because it was not clear to me if he wanted the file displayed in the
same window, or with a "Open/Save" dialog.

Greetings,
Laurent
 

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

Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,356
Latest member
deepthi.kodakandla

Latest Threads

Top