Re: Serve up file from outside web application directory
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Thu, 30 Aug 2007 02:42:54 GMT
Hi Brian,
Any progress on this or does the further info in my last reply still helps
some? If you have any further questions on this, please don't hesitate to
let me know.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
References: <#c5n#jB4HHA.4672@xxxxxxxxxxxxxxxxxxxx><iKGdnaUWU_Dw7lnbnZ2dnUVZ8sqjnZ2d@xxxxxx>
<t-udnbOodYIv7lnbnZ2dnUVZ8qeknZ2d@xxxxxx>
<F5HBEhH4HHA.6140@xxxxxxxxxxxxxxxxxxxxxx>
<OBnTnXb5HHA.5984@xxxxxxxxxxxxxxxxxxxx>
<STsz1Vf5HHA.2340@xxxxxxxxxxxxxxxxxxxxxx>
<#MGOOWl5HHA.3940@xxxxxxxxxxxxxxxxxxxx>
I
Hi Brian,
Thanks for your followup.
I've rechecked the code and it does be a mistake in my code as I used
"Response.Write" to write out the byte[](binary data), this is incorrect.
should use the "BinaryWrite" method for binary data(byte[] array).microsoft.public.dotnet.framework.aspnet:39662
"Response.Write" should be used for string text data.
Here I've pasted two methods both of which can do the work(one write out
binary data and another write out string data), you can have a look and
choose either one as you want:
protected void BinaryWriteMethod()binary approach>>>>>>>>>
{
byte[] bytes = null;
string path = "e:\\temp\\temp.txt"; // This file exists and has
one sentence in it: The quick brown fox...etc.
bytes = System.IO.File.ReadAllBytes(path);
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "text/plain";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("Content-disposition", "attachment;
filename=sometext.txt");
Response.BinaryWrite(bytes);
Response.End();
}
text string approach>>>>>>>>
protected void StringWriteMethod()
{
string path = "e:\\temp\\temp.txt"; // This file exists and
has one sentence in it: The quick brown fox...etc.
StreamReader sr = new StreamReader(path, Encoding.UTF8);
string data = sr.ReadToEnd();
sr.Close();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "text/plain";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("Content-disposition", "attachment;
filename=sometext.txt");
Response.Write(data);
Response.End();
}
BTW, for string text approach, you need to make sure the text
encoding(specified in code) matches the text file's text encoding.
Hope this helps. If there is still anything unclear, please don't hesitate
to let me know.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Brian Simmons" <centraso@xxxxxxxxxxxxxxxx><iKGdnaUWU_Dw7lnbnZ2dnUVZ8sqjnZ2d@xxxxxx>
References: <#c5n#jB4HHA.4672@xxxxxxxxxxxxxxxxxxxx>
<t-udnbOodYIv7lnbnZ2dnUVZ8qeknZ2d@xxxxxx>
<F5HBEhH4HHA.6140@xxxxxxxxxxxxxxxxxxxxxx>
<OBnTnXb5HHA.5984@xxxxxxxxxxxxxxxxxxxx>
<STsz1Vf5HHA.2340@xxxxxxxxxxxxxxxxxxxxxx>
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: <#MGOOWl5HHA.3940@xxxxxxxxxxxxxxxxxxxx>
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
oneX-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
meansentence 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
"Steven Cheng[MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:STsz1Vf5HHA.2340@xxxxxxxxxxxxxxxxxxxxxxxxx
Thanks for your followup Brian,
for the text file with "System.Byte[]" problem you mentioned, do you
havethe file download dialog display the filename as "System.Byte[]"?
If this is the case, I suggest you check the code to see whether you
whenadd the "Content-Disposition" header and supply the filename. Because
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.
.
- Follow-Ups:
- Re: Serve up file from outside web application directory
- From: Brian Simmons
- Re: Serve up file from outside web application directory
- References:
- Serve up file from outside web application directory
- From: Brian Simmons
- Re: Serve up file from outside web application directory
- From: Aidy
- Re: Serve up file from outside web application directory
- From: Aidy
- Re: Serve up file from outside web application directory
- From: Steven Cheng[MSFT]
- Re: Serve up file from outside web application directory
- From: Brian Simmons
- Re: Serve up file from outside web application directory
- From: Steven Cheng[MSFT]
- Re: Serve up file from outside web application directory
- From: Brian Simmons
- Re: Serve up file from outside web application directory
- From: Steven Cheng[MSFT]
- Serve up file from outside web application directory
- Prev by Date: Re: ASPNet2.0 IIS6 file download question
- Next by Date: Re: Flat files and ASP.NET
- Previous by thread: Re: Serve up file from outside web application directory
- Next by thread: Re: Serve up file from outside web application directory
- Index(es):
Relevant Pages
|