在我的控制器中,我想创建一个pdf文件,保存并仅在出现错误时知道,我不想下载该文件
public ActionResult CreatePdf(string IDUser, int IDCourse)
{
try
{
string path = Server.MapPath("~/Public/Pdf/");
var fileName = Guid.NewGuid().ToString();
Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 15);
pdfDoc.SetPageSize(PageSize.A4.Rotate());
var pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
var fileStream = new FileStream(Path.Combine(path, fileName), FileMode.Create);
pdfWriter = PdfWriter.GetInstance(pdfDoc, fileStream);
pdfDoc.Open();
...
pdfWriter.CloseStream = false;
pdfDoc.Close();
pdfWriter.Close();
fileStream.Close();
db.SaveChanges();
//Response.Buffer = true;
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=\"" + fileName + "\"");
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.Write(pdfDoc);
//Response.End();
//return File(Response.OutputStream, "application/pdf", fileName.ToString()); // if I want download
return Content("ok");
catch (Exception ex)
{
return Content("error");
}
}
一开始我确实下载了pdf,现在我循环调用此过程来创建更多的pdf,所以我只想知道创建中是否有错误。
从我调用ajax(jquery)该控制器的视图中
public ActionResult Create(string listIDUser, int IDCourse)
{
string msg = string.Empty;
try
{
var IDUser = listIDUser.Split(',');
foreach (var item in IDUser)
{
var pdf= CreatePdf(item, IDCourse);
if (pdf!= null)
{
...
}
else
{
return new JsonResult() { Data = new { result = "err", msg = "..." + item } };
}
}
return new JsonResult() { Data = new { result = "ok", msg = "Perfect!" } };
}
catch (Exception ex)
{
return new JsonResult() { Data = new { result = "err", msg = ex.Message } };
}
}
进行循环并调用CreatePdf控制器,但在带萤火虫的浏览器中,pdf文件返回,而不是我期望的json中的答案! 谢谢