C# PDF 预览
public ActionResult V1() { try { string filePath = "E://123.pdf"; if (System.IO.File.Exists(filePath) == false) { return base.Content("文件不存在!"); } Response.ContentType= "application/pdf"; Response.AddHeader("Accept-Ranges", "bytes"); Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition"); Response.AddHeader("Connection", "keep-alive"); Response.AddHeader("Content-Disposition", "inline; filename=" + id+".pdf"); Response.WriteFile(filePath); Response.End(); return base.Content(""); } catch (Exception e) { return base.Content(e.Message); } }
public ActionResult V2() { try { string filePath = "E://123.pdf"; if (System.IO.File.Exists(filePath) == false) { return base.Content("文件不存在!"); } var memoryStream = new MemoryStream(); using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { stream.CopyTo(memoryStream); } memoryStream.Seek(0, SeekOrigin.Begin); Response.AddHeader("",""); return new FileStreamResult(memoryStream, "application/pdf"); } catch (Exception e) { return base.Content(e.Message); } }