我的问题:
如果图像与Postman一起使用,最有可能出现图像解析失败的地方。如果问题是html的解析,那我将如何最好地提供html并保持图像完整(如邮递员html休息)。
注意:
我在使用iTextsharp解析html内容时遇到问题
使用基于休息的服务。
信息
1.邮递员-HTML数据一切正常。
2. JSON发布-图片丢失。
3.作为没有WCF / Rest的课程**相同问题**
我认为我的解析存在问题,但是源显示与邮递员提交的相同。
HTML
<html>
<head>
<title>Test</title>
</head>
<body>
<img src='/assets/images/logo.png'/><br />
<h1>This is a test</h1>
</body>
</html>
页面代码
$(document).ready(function () {
var html_data = JSON.stringify("<html><head><title>test</title></head><body><img id='test_image' src='/assets/images/logo.png'/>This is a test</body></html>");
var obj = JSON.stringify({html:html_data,filename:filename});
Ajax(data,"NameOfPdf.pdf",emailobj);
});
function Ajax(html, filename) {
return $.ajax({
url: "/_services/PdfManager.svc/pdf/fromhtml",
contentType: "application/json",
dataType: "json",
type: "POST",
async: true,
data: JSON.stringify({ html, "filename": filename})
});
};
** C#/ EndPoint **
public string CreatePDFFromHTML(string html, string filename)
{
log.InfoFormat("Html:{0}, File:{1}", html, filename);
dynamic status = HttpStatusCode.Conflict;
try
{
filename = HttpContext.Current.Server.MapPath(String.Format("/_files/{0}", filename));
object TargetFile = filename;
byte[] file = null;
// do some additional cleansing to handle some scenarios that are out of control with the html data
string HtmlData = html.Replace("<br>", "<br />");
// create a stream that we can write to, in this case a MemoryStream
using (var stream = new MemoryStream())
{
// create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
var bytes = System.Text.Encoding.UTF8.GetBytes(HtmlData);
using (var input = new MemoryStream(bytes))
{
var output = new MemoryStream(); // this MemoryStream is closed by FileStreamResult
var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 50, 50, 50, 50);
var writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;
document.Open();
var xmlWorker = XMLWorkerHelper.GetInstance();
xmlWorker.ParseXHtml(writer, document, input, Encoding.UTF8);
document.Close();
output.Position = 0;
MemoryStream ms = new MemoryStream();
output.CopyTo(ms);
file = ms.ToArray();
}
}
File.WriteAllBytes(TargetFile.ToString(), file);
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
string strHtml = string.Empty;
string pdfFileName = filename;
//email = object predefined elsewhere (object for use with AmazonSES as EmailerService - can be SMTP or otherwise - not essential to functionality and has been ommitted from code).
if (!String.IsNullOrEmpty(email))
{
log.Debug(email);
(new EmailerService()).SendEmailWithAttachment(email, pdfFileName);
status = HttpStatusCode.OK;
}
}
catch (Exception ex)
{
status = new { Status = HttpStatusCode.InternalServerError, Messsage = ex.Message, StackTrace = ex.StackTrace };
log.ErrorFormat("{0}:{1}", ex.Message, ex.StackTrace);
}
return JsonConvert.SerializeObject(status);
}