我已经创建了一个Web应用程序,它将PDF文件发送到SQL数据库表,以便稍后加载并通过邮箱发送.目前,我的代码工作,PDF文件是通过邮箱发送的,然而,一旦打开PDF,所有显示如预期,除了背景图像,这不再出现在PDF上.现在我不明白的是,如果我在发送到数据库之前使用‘IFormFile’,PDF文件在通过邮箱发送时会正确显示,并包含其背景图像.使我认为我一定是错误地将PDF文件转换为二进制格式或从二进制格式转换为PDF文件?
请查看我当前的代码:
到控制器的JavaScript AJAX: ajax个
if(email) {
var file = doc.output('blob');
var fd = new FormData();
fd.append('pdfFile',file);
fd.append('jobNo',$('#jobNoInput').val());
$.ajax({
url: "/Jobs/SendInvoiceEmailToQueue",
data: fd,
dataType: 'text',
processData: false,
contentType: false,
type: 'POST',
success: function(data) {
var data2 = JSON.parse(data);
if(data2 == "SUCCESS")
{
quickToast('Invoice added to Email Queue for Job
No: ' + $('#jobNoInput').val(), 'success', 'Added to Email Queue');
}
}
});
} else {
doc.save('Invoice.pdf');
}
将PDF上传到数据库: upload pdf to database个
public IActionResult SendInvoiceEmailToQueue(string jobNo)
{
var stream = Request.BodyReader.AsStream();
IFormFile pdfFile = Request.Form.Files[0];
string fileName = pdfFile.FileName;
var result = new StringBuilder();
using (var reader = new StreamReader(pdfFile.OpenReadStream()))
{
result.Append(reader.ReadToEnd());
}
string base64 = result.ToString();
Byte[] bitmapData = new Byte[base64.Length];
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(base64);
connectionString();
con.Open();
com.Connection = con;
var query = "INSERT INTO dbo.email_queue([JOBNO],[PDF],[EMAILADDRESS],[DATE],[TIME]) VALUES(@jobNo, @pdf, 'address', GETDATE(), CURRENT_TIMESTAMP)";
SqlParameter picparameter = new SqlParameter();
picparameter.SqlDbType = SqlDbType.Image;
picparameter.ParameterName = "pdf";
picparameter.Value = plainTextBytes;
SqlParameter jobNoparameter = new SqlParameter();
jobNoparameter.SqlDbType = SqlDbType.Int;
jobNoparameter.ParameterName = "jobNo";
jobNoparameter.Value = Int32.Parse(jobNo);
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add(picparameter);
cmd.Parameters.Add(jobNoparameter);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
con.Dispose();
return new JsonResult("SUCCESS");
}
通过邮箱检索和发送PDF: enter image description here个
public IActionResult SendInvoiceEmail(string queueNo)
{
connectionString();
con.Open();
com.Connection = con;
com.CommandText = "SELECT * FROM dbo.recovery_email_queue WHERE EmailQueueRecNo = '" + queueNo + "'";
dr = com.ExecuteReader();
while (dr.Read())
{
var imageSource = System.Text.Encoding.UTF8.GetString(((byte[])dr["PDF"]));
var pdffile = (byte[])dr["PDF"];
var pdfBytes = Encoding.UTF8.GetBytes(imageSource);
var stream = new MemoryStream(pdfBytes);
IFormFile file = new FormFile(stream, 0, pdfBytes.Length, "invoice", "invoice.pdf");
//attach pdf
var attachment = new Attachment(file.OpenReadStream(), "invoice.pdf");
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("fromAdd");
mail.To.Add("add");
mail.Subject = "Test Sending Invoice";
mail.Body = "<h1>This is body</h1>";
mail.IsBodyHtml = true;
mail.Attachments.Add(attachment);
using (SmtpClient smtp = new SmtpClient("smtp-mail.outlook.com", 587))
{
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["emailUser"].ToString(), ConfigurationManager.AppSettings["emailPass"].ToString());
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
}
return new JsonResult("SUCCESS");
}
数据库列‘PDF’的类型为varbinary(MAX)
使用jsPDF生成的PDF文件