Click here to Skip to main content
12,068,967 members (65,344 online)
Rate this:
 
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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 20 Jun 2013
Copyright © CodeProject, 1999-2016
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