view a file in ASP.NET

  • Thread starter Thread starter Thelak
  • Start date Start date
Thelak said:
How do I view a text file after I created the file in
ASP.NET?

You're going to get many different responses to your question.
Why don't you just Response.Redirect the browser to the text file?

Or do you want to get more elaborate and read the file into a buffer and
Response.BinaryWrite() the buffer like this:?
the following is for reading a pdf file on the server.
private void Page_Load(object sender, System.EventArgs e){
if(Request.QueryString["encryptedFilename"]!=null &&
Request.QueryString["encryptedFilename"] != ""){
string fileName = (new
SymmCrypto(SymmCrypto.SymmProvEnum.RC2)).Decrypting(Request.QueryString["enc
ryptedFilename"],"myEncryptionKey");
System.IO.FileStream stream = System.IO.File.OpenRead(fileName);
byte[] buffer = readFully(stream);
stream.Close();
File.Delete(fileName);
sendToBrowser(buffer);
}
}
private byte[] readFully(System.IO.Stream stream){
byte[] buffer = new byte[32768];
using(MemoryStream ms = new MemoryStream()){
while(true){
int read = stream.Read(buffer,0,buffer.Length);
if(read <= 0){
return ms.ToArray();
}
ms.Write(buffer,0,read);
}
}
}
private void sendToBrowser(byte[] buffer){
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","inline;filename=dummy.pdf");

Response.BinaryWrite(buffer);
Response.Flush();
Response.Close();
}
 
Back
Top