Click here to Skip to main content
11,798,046 members (77,316 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET PDF file
I am have on table which stores uploaded file with guid and file also saved with guid name.
I want to send this file as attachment. When I send file as it is in attachment with guid name then it is working fine.(below code works great)

Attachment attachResource = new Attachment(Server.MapPath("/public/_pdf/" + strFILEGUID + ".pdf"));

But i want to send this file with some different name so i used below code.

System.IO.MemoryStream data = new System.IO.MemoryStream();
System.IO.Stream str = File.OpenRead(Server.MapPath("/public/_pdf/" + strFILEGUID  + ".pdf"));
data.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
Attachment attachResource = new Attachment(data, "ChangedFileName" + ".pdf");

This code send file with name 'ChangedFileName.pdf' but while opening file it says file is damaged. I am not getting the problem here. Any help will be appreciated.
Posted 20-Jun-13 1:20am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use BinaryReader to read the PDF File
System.IO.BinaryReader bw = new BinaryReader(str);
byte[] buf = bw.ReadBytes((int)str.Length);
pradiprenushe at 20-Jun-13 7:37am
Thank you very much. It works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 575
1 Maciej Los 375
2 OriginalGriff 353
3 Abhinav S 342
4 Richard MacCutchan 218
0 OriginalGriff 2,150
1 Maciej Los 1,790
2 KrunalRohit 1,456
3 CPallini 1,415
4 Richard MacCutchan 1,016

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100