我关注的三个步骤。
varbinary(max)
创建PDF:
public byte[] PDF(List<DeliveryOrderDetail> deliveryOrdList)
{
byte[] result;
string headingText = "Guide";
try
{
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document();
PdfWriter.GetInstance(doc, ms).CloseStream = false;
PdfPTable pdfTab = new PdfPTable(3);
Paragraph heading = new Paragraph(headingText, new Font(Font.TIMES_ROMAN, 18f, Font.NORMAL, Color.BLACK));
Paragraph ItemDetail = new Paragraph("Items in Delivery Order Box:", new Font(Font.TIMES_ROMAN, 12f, Font.NORMAL, Color.BLACK));
heading.Alignment = Element.ALIGN_CENTER;
ItemDetail.Alignment = Element.ALIGN_CENTER;
doc.Open();
doc.Add(heading);
pdfTab.HorizontalAlignment = 1; // 0- Left, 1- Center, 2- right
pdfTab.SpacingBefore = 20f;
pdfTab.SpacingAfter = 20f;
// pdfTab.AddCell("Sl. No.");
pdfTab.AddCell("ItemName");
pdfTab.AddCell("BoxId");
pdfTab.AddCell("Manufacturer");
doc.Add(ItemDetail);
foreach (var item in deliveryOrdList)
{
pdfTab.AddCell(item.Name);
pdfTab.AddCell(item.Id.ToString());
pdfTab.AddCell(item.Manufacturer);
}
doc.Add(pdfTab);
doc.Close();
result = ms.GetBuffer();
}
return result;
}
catch(Exception e )
{
}
return null;
}
使用db.savechanges();
点击图片
var pdf will get the database field which contains binary data
jQuery: when image is clicked go to controller
<script type="text/javascript">
$(document).ready(function () {
$('.Pdf').click(function () {
var pdf = $(this).closest('tr').find('td:eq(0) input').val();;
alert(pdf);
$.ajax({
url: "/ship/PDF",
type: "POST",
dataType: "json",
data:JSON.stringify({ pdf: pdf }),
//data:{pdf:pdf},
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8"
})
})
})
</script>
在Controller中:编写了以下方法,接受为字符串:
[HttpPost]
public ActionResult PDF(string pdf)
{
try
{
byte[] byteInfo = System.Text.Encoding.UTF8.GetBytes(pdf);
MemoryStream ms = new MemoryStream();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return new FileStreamResult(ms, "application/pdf");
}
//to catch the exception
catch (Exception _Exception)
{
}
return null;
}
在点击图片时,我无法看到pdf.in
浏览器。我已经尝试了所有选项,但无法得到它。如果我把它写在文件中。 PDF将无法打开
System.IO.File.WriteAllBytes(@"D:\personal\testpdf.pdf", byteInfo);
答案 0 :(得分:0)
您向控制器发送请求的方式只会发回 //文件供下载但不会在窗口中打开。我不确定是否能生成// PDF文件。首先应该是您能够生成正确的PDF //文件。如果你想实现这一点,当用户点击并下载文件时,你应该这样做。
public ActionResult DownLoadFile()
{
string filePath = Server.MapPath("~/PDFFOLDER/YorFile.pdf");
var bytes = System.IO.File.ReadAllBytes(filePath);
return File(bytes, "application/pdf", "yourfilename.pdf");
//Another way can be using direct stream
// var bytes = as you are getting these bytes from DB;
return File(bytes, "application/pdf", "yourfilename.pdf");
}
如果你想在新标签中打开文件,那么你可以在服务器上做这样的事情并在一些文件夹中写下你的实际文件。 //System.IO.File.WriteAllBytes(Server.MapPath("~/PDFFOLDER/YorFile.pdf“),byteInfo);
function ViewPdfInNewWindow(filename)
{
var linkHref = "www.mysite.com";
var str = linkHref.toString();
linkHref = str + "/PDFFOLDER/" + filename;
childWindow = window.open(linkHref); //Open in new window
}
Let me know if it works !