Subject: Re: Serve up file from outside web application directory
Date: Fri, 24 Aug 2007 09:48:41 -0400
Lines: 84
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138
X-RFC2646: Format=Flowed; Original
Message-ID: <#
[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 65.124.198.22
Path: TK2MSFTNGHUB02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP05.phx.gbl
Xref: TK2MSFTNGHUB02.phx.gbl microsoft.public.dotnet.framework.aspnet:39662
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
Hi Steven,
Here is my exact code from the Page_Load event (sometext.aspx):
byte[] bytes = null;
string path = "C:\\sometext.txt"; // This file exists and has one
sentence in it: The quick brown fox...etc.
bytes = File.ReadAllBytes(path);
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "text/plain";
Response.AddHeader("Content-disposition", "attachment;
filename=sometext.txt");
Response.Write(bytes);
Response.End();
How I'm Calling that sometext.aspx page:
<a href='sometext.aspx' target='_blank'>Some Text</a>
What happens when that link is clicked:
A new browser window is spawned. IE7 displays a dialog:
Do you want to open or save this file?
If you click open, Notepad displays and the text inside Notepad is:
System.Byte[]
If you click save (to your Desktop let's say), the file saves, and it's
contents are:
System.Byte[]
Any ideas?
Thanks,
Brian
Thanks for your followup Brian,
for the text file with "System.Byte[]" problem you mentioned, do you mean
the file download dialog display the filename as "System.Byte[]"?
If this is the case, I suggest you check the code to see whether you have
add the "Content-Disposition" header and supply the filename. Because when
you stream out a file(or other binary content), the ASP.Net won't care
what's their original filename, you need to explicitly supply them.e.g.
======================
protected void Page_Load(object sender, EventArgs e)
{
byte[] bytes = null;
string path = @"d:\filestore\pdf_files\test.pdf";
bytes = File.ReadAllBytes(path);
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType= "text/plain";
//here put the filename
Response.AddHeader("Content-disposition", "attachment;
filename=test.txt");
Response.Write(bytes);
Response.End();
}
===========
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.