.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Writing a file to the browser as an attatchment

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

This code shows how to send a file as an attachment to browser

string sContent;
sContent = "Content-type: text/xml";
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment; filename=example.xml");
Response.Charset = "UTF-8";
Response.ContentType = sContent;

FileStream OtStream;
outputStream1 = new FileStream(@"C:example.xml", FileMode.Open, FileAccess.Read);
MemoryStream mstream;
ms = new MemoryStream((int)otStream.Length);
BinaryReader breader;
br = new BinaryReader(OtStream);
byte[] bytesReader = breader.ReadBytes((int)OtStream.Length);
ms.Write(bytesReader,0,(int)OtStream.Length);
Response.OutputStream.Write(mstream.ToArray(), 0, (int)mstream.Length);
OtStream.Close();
mstream.Close();
breader.Close();


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend