有需求.net core项目中使用docx-preview.js预览Word文档,网上很多资料,主要是Vue如何使用docx-preview预览Word文档,很少HTML网页中使用,一般网页都是预览后端的某文件,网上配合后端的资料很少,搜到的也用不成,我掉了一些头发弄了出来,记录一下,希望对其他人也有帮助。
这个插件只支持docx文件,doc文件我写了转换,使用Aspose.word转换一下。
需要下载:jszip.min.js和docx-preview.js库,直接从官网Demo页面,打开开发者工具另存为到我的电脑就行了。
官网Demo:https://volodymyrbaydalka.github.io/docxjs/
需要代码的去下载:https://download.csdn.net/download/u011964241/87020693
前端HTML网页代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="~/js/jszip.min.js"></script> <script src="~/js/docx-preview.js"></script> </head> <body > <div> <button onclick="loadocx()">点击预览docx文件</button> <button onclick="loaddoc()">点击预览doc文件</button> </div> <div> <div id="document-container" ></div> </div> <script> var ajax = function (option) { var xhr = new XMLHttpRequest(); xhr.open(option.type, option.url, option.async); xhr.responseType = option.dataType; xhr.onload = function (e) { if (this.status == 200) { option.success(this); } }; xhr.onerror = function (ex) { option.error(ex); } xhr.send(); } var $ = { ajax: ajax }; function loadocx() { $.ajax({ type: 'get', url: '@Url.Content("~/Home/FileBlob")', async: true, dataType: 'arraybuffer', success: function (data) { data = data.response; var container = document.querySelector("#document-container"); docx.renderAsync(data, container, null, docx.defaultOptions); }, error: function (ex) { alert('error'); } }); } function loaddoc() { $.ajax({ type: 'get', url: '@Url.Content("~/Home/FileDocBlob")', async: true, dataType: 'arraybuffer', success: function (data) { data = data.response; var container = document.querySelector("#document-container"); docx.renderAsync(data, container, null, docx.defaultOptions); }, error: function (ex) { alert('error'); } }); } </script> </body> </html>
后端关键代码:
先在nuget中安装aspose.word,skiasharp.
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29[HttpGet] public IActionResult FileBlob() { var file = @"D:UsersitliuDesktop隧洞接口.docx"; //将文件转换为二进制返回前端 FileStream fs = new FileStream(file, FileMode.Open); byte[] bs = new byte[fs.Length]; fs.Read(bs, 0, (int)fs.Length); fs.Close(); FileContentResult result = new FileContentResult(bs, "application/octet-stream"); return result; } [HttpGet] public IActionResult FileDocBlob() { var file = @"D:UsersitliuDesktop收入证明.doc"; var outputfile = @"D:UsersitliuDesktop收入证明.docx"; //前端不支持doc文件,将doc文件转成docx文件 var doc = new Aspose.Words.Document(file); doc.Save(outputfile, Aspose.Words.SaveFormat.Docx); //将文件转换为二进制返回前端 FileStream fs = new FileStream(outputfile, FileMode.Open); byte[] bs = new byte[fs.Length]; fs.Read(bs, 0, (int)fs.Length); fs.Close(); FileContentResult result = new FileContentResult(bs, "application/octet-stream"); System.IO.File.Delete(outputfile);//删除生成的文件 return result; }
最后
以上就是伶俐蜜蜂最近收集整理的关于docx-preview.js与.netCore后端实现预览Word文档的全部内容,更多相关docx-preview内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复