1

你好我有这个问题:

我有一个办公室插件(word);

我想将当前文件(.docx)的副本发送到 C# 控制器,我现在有这个代码,在代码的这个阶段我在“你的文件”中得到一个字符数组或一些 this,我如何得到一个 .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();
        }
4

2 回答 2

1

聚会迟到了,但我还是在这里添加了答案,以防其他人在以后需要它。

而不是使用 myEncodeBase64 你应该使用

var fileData = OSF.OUtil.encodeBase64(data);

它是 Office API 的一部分,因此您无需定义其他任何内容。

于 2016-04-27T11:50:35.013 回答
0

我一直在努力构建一个正确的 pptx 服务器端。最终这就是我想出的。

Javascript

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# ApiController

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; }
}

请注意:仅将此示例用作快速起点,因为您不想将字节保存在静态 List Document中。相反,您想让您的网络服务器无状态。

于 2019-04-09T12:25:16.347 回答