我有一个 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/