你好,我有这个问题:
我有一个办公室(单词)的插件;
我想将当前文件 (.docx) 的副本发送到 C# 控制器,我现在有这段代码,在代码的这个阶段,我得到一个字符数组或"yourfile"中的一些,我如何得到一个.docx文件?
JavaScript
function sendFile() {
Office.context.document.getFileAsync("compressed",
{ sliceSize: 100000 },
function (result) {
if (result.status == Office.AsyncResultStatus.Succeeded) {
var myFile = result.value;
var state = {
file: myFile,
counter: 0,
sliceCount: myFile.sliceCount
};
getSlice(state);
}
});
}
function getSlice(state) {
state.file.getSliceAsync(state.counter, function (result) {
if (result.status == Office.AsyncResultStatus.Succeeded) {
sendSlice(result.value, state);
}
});
}
function myEncodeBase64(str)
{
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function sendSlice(slice, state) {
var data = slice.data;
if (data) {
var fileData = myEncodeBase64(data);
var _url = '../../api/file';
useAjax(_url, 'Post', JSON.stringify(fileData), _contentType).done(function (data) {
writeData(data);
app.showNotification("Translation was successfully done!");
});
}
}
和 C# 控制器:
public static string filemame = @"c:yourfile";
[Route("file")]
[HttpPost]
public void getFile([FromBody] string data)
{
Base64ToFile(data,filemame);
}
public static void Base64ToFile(string base64String, string filename)
{
byte[] fileByteArray = Convert.FromBase64String(base64String);
// Instantiate FileStream to create a new file
System.IO.FileStream writeFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
// Write converted base64String to newly created file
writeFileStream.Write(fileByteArray, 0, fileByteArray.Length);
// Clean up / disposal
writeFileStream.Close();
}
派对迟到了,但我还是在这里添加了答案,以防其他人在以后需要它。
与其使用 myEncodeBase64,不如使用
var fileData = OSF.OUtil.encodeBase64(data);
它是 Office API 的一部分,因此您不必定义任何其他内容。
我一直在努力构建一个正确的pptx服务器端。最终这就是我想出的。
爪哇语
function sendSlice(slice, state) {
var data = slice.data;
if (data) {
var isLastSlice = state.counter >= (state.sliceCount -1);
var ajaxData = {
isLastSlice: isLastSlice,
counter: state.counter,
documentData: btoa(data)
}
$.ajax({
url: "/api/Constructpptx", method: "POST", data: ajaxData, success: function (result) {
state.counter++;
if (isLastSlice) {
closeFile(state);
}
else {
getSlice(state);
}
}, error: function (xhr, status, error) {
}
});
}
}
作为 API 后端,我使用它C# API 控制器
public class ConstructpptxController : ApiController
{
public static List<byte> Document { get; set; } = new List<byte>();
public string Post([FromBody]ConstructpptxPayload payload)
{
if (payload.counter == 0)
Document.Clear();
var payloadData = Convert.FromBase64String(payload.documentData);
var pptBytes = Encoding.UTF8.GetString(payloadData).Split(',').Select(byte.Parse).ToArray();
Document.AddRange(pptBytes);
if(payload.isLastSlice)
{
var path = @"C:/Some/Local/Path/Presentation.pptx";
var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
fileStream.Write(Document.ToArray(), 0, Document.Count());
fileStream.Close();
Document.Clear();
}
return $"Counter: {payload.counter}, isLast: {payload.isLastSlice}, docLength: {Document.Count}";
}
}
public class ConstructpptxPayload
{
public bool isLastSlice { get; set; }
public int counter { get; set; }
public string documentData { get; set; }
}
请注意:仅将此示例用作快速起点,因为您不希望将字节保存在静态列表文档中。相反,您希望使您的Web服务器无状态。