最近再搞PDF得展示問題,因為aspose.pdf成本太高,只能使用pdf.js這個開源強大的前端東東了。
在百度了很久后 網上大都是node,java,php的事例,有位大哥的是C#的后台代碼按他寫的前端不生效,最終代碼更改如下
public void GetDoc(string id) { try { var model = zSWD_DocService.GetModelById(id); var path = System.Web.HttpContext.Current.Server.MapPath(model.DocFileUrl); using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { FileInfo fi = new FileInfo(path); Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.AppendHeader("Access-Control-Allow-Origin", "*"); Response.AppendHeader("Content-Length", fi.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(path); Response.Flush(); Response.End(); } } catch (Exception ex) { } }
其中一篇文章可以參考 https://blog.csdn.net/u013132051/article/details/76066920
另我的項目中,沒有像參考文檔中加上http的 是可以直接虛路徑的 Scripts/PDFJS/web/viewer.html?file=%2FZSWDManage%2Fdoc%2FGetDoc%3Fid%3D7237fe80-9762-4178-9244-7d05adfa47fd
其他的一些修改
1.中文顯示問題 修改 <link rel="resource" type="application/l10n" href="locale/locale.properties"> 為 <link rel="resource" type="application/l10n" href="locale/locale.txt">
並注釋
x.responseType = 'moz-chunked-arraybuffer';
2.默認url 修改為“”
參考 http://www.bubuko.com/infodetail-2727204.html
實現效果