【发布时间】:2016-11-11 06:58:25
【问题描述】:
我正在尝试将 imageData 保存为服务器目录上的 pdf 文件。 Html5Canvas imageData 被发送到服务器并在字节数组中转换后,尝试保存为 PDF 文件。文件在指定路径上成功生成,但生成的文件无法在大多数 PDF 阅读器(即 Adobe Reader、Foxit 阅读器等)中正确打开,并显示文件已损坏或损坏但在 MS Edge 浏览器中正确打开的错误。我也希望它们在普通的 PDF 阅读器中显示。您能否提出解决方案。这是我的服务器端代码。
public static string SaveImage(string imageData, string userEmail, int quantity)
{
string completePath = @"~\user-images\";
string imageName = "sample_file2.pdf";
string fileNameWitPath = completePath + imageName;
byte[] bytes = Convert.FromBase64String(imageData);
File.WriteAllBytes(HttpContext.Current.Server.MapPath(fileNameWitPath), bytes);
}
为此代码生成相同的输出
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(fileNameWitPath), FileMode.OpenOrCreate);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
也为此。
using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(fileNameWitPath), FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
【问题讨论】:
-
那么您是在浏览器上创建 PDF 吗?由于您没有在此处进行任何转换
标签: c# pdf binary-data