c# - 我可以使用 Base64String 中的 body.insertFileFromBase64 来自 doc 文件而不是 docx

标签 c# ms-word office365 office-js

我有一个 web 服务,它读取 .doc 或 .docx 并通过 ajax 将其 Base64String 发送到我的插件中 这是网络服务的代码

public string ReadDocument(string path)
        {

            FileStream fsStream = null;
            BinaryReader objReader = null;
            try
            {


                ////////////////////////

                // Now, read binary file
                //path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Copy.docx";
                fsStream = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte[] fileContent = new byte[fsStream.Length];
                objReader = new BinaryReader(fsStream);
                objReader.Read(fileContent, 0, fileContent.Length);
                //toRet.FileContent = fileContent;
                //toRet.FileName = "Test.doc";
                string StrBase64 = Convert.ToBase64String(fileContent);
                string _Document = StrBase64 ;
                return _Document;
                // FlushResponse(_Document, "application/pdf");
            }
            catch (Exception ex)
            {
                // FlushResponse("error");
                return "Error " + ex.Message;
            }
            finally
            {
                if (objReader != null) objReader.Close();
                if (fsStream != null) fsStream.Close();
            }
        }

当我的 Base64String 来自 .docx 时,我遇到了 body.insertFileFromBase64 的问题,但当 Base64String 来自 .doc 时,它不起作用 这是我在插件中的代码,我发送结果参数,它是来自 ajax 的 Base64String

function InsertDocument(result) {
    Word.run(function (context) {

        // Create a proxy object for the document body.
        var body = context.document.body;
        body.clear();
        // Queue a commmand to insert base64 encoded .docx at the beginning of the content body.
        // You will need to implement getBase64() to pass in a string of a base64 encoded docx file.
        body.insertFileFromBase64(result, Word.InsertLocation.start);

        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log('Added base64 encoded text to the beginning of the document body.');
            $("#loading").hide();
        });
    })

有什么想法我可以通过office.js或任何其他功能将.doc插入office 2016

最佳答案

body.insertFileFromBase64的引用方法意味着第一个参数必须是 docx 文件:

base64File string Required. The base64 encoded content of a .docx file

.

关于c# - 我可以使用 Base64String 中的 body.insertFileFromBase64 来自 doc 文件而不是 docx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44354396/

相关文章:

vba - 从具有合并单元格的表格中删除范围

vba - 通过查找函数进行字数统计返回的结果与宏不同

c# - telerik winforms linq 到 radgridview

c# - 可以使用反射来实例化对象基类属性吗?

c# - 重构 LINQ ForEach 以返回 IEnumerable<T>

C# - 在循环中启用多个文本框

java - 使用来自 java 对象的值从模板动态创建 word 文档

excel - Office 2016 -> 2013 "compile error, can' t 查找项目或库”

ios - 使用适用于 iOS 的 Office 365 SDK 读取文件

c# - Outlook 电子邮件中 ​​HTML 页面的格式丢失