Home | Articles|Namespace|Interview Questions|Tools|Jobs|Projects|Community
Asp.net Tutorials

»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books

»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects

»Competition
Community
Winners
Prizes
Write For Us
Members

»Other Resources
Links
Dotnet Resources

Writing a file to the browser as an attatchment

This code shows you how to write a file to the browser as an attatchement which then can be saved by the user

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

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

Have a Question and dont know the answer post it below and get answers in minutes

Due to spam this feature is disabled
To get answers fast , make sure you enter a detailed subject for example: "DataGrid issues need answer" not "DataGrid"

Subject:

Catjegory Name:

Message:



© 2008 dotnetwatch.com -- Privacy policy

Website Design & Internet Marketing by Shivam