Binary Write in ASP.net

Probably you have seen a lot of websites which have download facilities. You can give the link of the file in your site which can be a threat for your site because, the user can be able to know where have you put the file.Then what can be the solution?

You can use BINARYWRITE in asp.net which will solve the above problem. Here is the  code for BinaryWrite

FileStream MyFileStream = new FileStream(@”d:\inetpub\wwwroot\Afile.pdf”, FileMode.Open);
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType=”application/pdf”;
Response.AddHeader( “content-disposition”,”attachment; filename=AnotherPdf.PDF”);
Response.BinaryWrite(Buffer);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s