Pdf附件损坏问题。 [英] Pdf attachment damaged problem.

查看:109
本文介绍了Pdf附件损坏问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在桌面上存储guid的文件,文件也用guid名称保存。

我想把这个文件作为附件发送。当我发送文件,因为它是附件与guid名称,然后它工作正常。(下面代码很好)



<前lang =c#>附件attachResource = new 附件(Server.MapPath( / public / _pdf / + strFILEGUID + 。pdf));
mail.Attachments.Add(attachResource);





但是我想发送一个不同名称的文件,所以我用过代码。



 System.IO.MemoryStream data =  new 系统.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);
附件attachResource = new 附件(data, ChangedFileName + 。pdf);
mail.Attachments.Add(attachResource);





此代码发送文件名为'ChangedFileName.pdf'但在打开时文件说文件已损坏。我在这里没有遇到问题。任何帮助将不胜感激。

谢谢

解决方案

使用BinaryReader阅读PDF文件

 System.IO.BinaryReader bw = new BinaryReader(str); 

byte [] buf = bw.ReadBytes((int)str.Length);



谢谢

--RA


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

解决方案

Use BinaryReader to read the PDF File

System.IO.BinaryReader bw = new BinaryReader(str);

byte[] buf = bw.ReadBytes((int)str.Length);


Thanks
--RA


这篇关于Pdf附件损坏问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆