Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET PDF file
Hi,
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"));
mail.Attachments.Add(attachResource);
 
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"));
str.CopyTo(data);
data.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
Attachment attachResource = new Attachment(data, "ChangedFileName" + ".pdf");
mail.Attachments.Add(attachResource);
 
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.
Thanks
Posted 20-Jun-13 2:20am

1 solution

Rate this: bad
good
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);
Thanks
--RA
  Permalink  
Comments
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 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2014
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