从C#以编程方式创建文件到Onedrive? [英] Create file to Onedrive programmatically from C#?

查看:317
本文介绍了从C#以编程方式创建文件到Onedrive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接从C#创建一个doc,docx,pptx或excel文件到我的Onedrive帐户. 我已经尝试过了,但是对我不起作用.有人知道我做错了什么吗?谢谢

I want to create a doc, docx, pptx or excel file from C# direct to my Onedrive account. I have try this but it's not working for me. Anybody have any idea what I did wrong ? Thanks

public async Task<ActionResult> CreateWordFile()
{
   LiveLoginResult loginStatus = await authClient.InitializeWebSessionAsync(HttpContext);
   if (loginStatus.Status == LiveConnectSessionStatus.Connected)
   {
        var fileData = new Dictionary<string, object>();
        fileData.Add("name", "Document.docx");
        fileData.Add("Content-Type", "multipart/form-data; boundary=A300x");
        fileData.Add("type", "file");

        LiveOperationResult getResult = await connectedClient.PostAsync("me/skydrive/files", fileData);
    }

    return View();
}

我得到的错误是这个:

EDITED: The error that I get is this one:

"标头"Content-Type"缺少必需的参数"boundary". 说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息. 异常详细信息:Microsoft.Live.LiveConnectException:标头"Content-Type"缺少必需的参数"boundary"."

"The header 'Content-Type' is missing the required parameter: 'boundary'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: Microsoft.Live.LiveConnectException: The header 'Content-Type' is missing the required parameter: 'boundary'."

推荐答案

最后,我从c#创建一个docx文件.我将解决方案放在这里(该方法中的代码未重构,因此可以将其拆分为严格的方法).

Finally I create a docx file from c#. I put here the solution (the code from the method is not refactored so it can be split in a severeal methods).

public async Task<ActionResult> CreateWordFile()
{
    LiveLoginResult loginStatus = await authClient.InitializeWebSessionAsync(HttpContext);
    if (loginStatus.Status == LiveConnectSessionStatus.Connected)
    {
        connectedClient = new LiveConnectClient(this.authClient.Session);
        string url = "https://apis.live.net/v5.0/me/skydrive/files?access_token=" + this.authClient.Session.AccessToken;

         MemoryStream streamDoc = new MemoryStream();
        DocX doc = DocX.Create(streamDoc);

        string headlineText = "Constitution of the United States";
        string paraOne = ""
            + "We the People of the United States, in Order to form a more perfect Union, "
            + "establish Justice, insure domestic Tranquility, provide for the common defence, "
            + "promote the general Welfare, and secure the Blessings of Liberty to ourselves "
            + "and our Posterity, do ordain and establish this Constitution for the United "
            + "States of America.";

        // A formatting object for our headline:
        var headLineFormat = new Formatting();
        headLineFormat.FontFamily = new System.Drawing.FontFamily("Arial Black");
        headLineFormat.Size = 18D;
        headLineFormat.Position = 12;

        // A formatting object for our normal paragraph text:
        var paraFormat = new Formatting();
        paraFormat.FontFamily = new System.Drawing.FontFamily("Calibri");
        paraFormat.Size = 10D;

        doc.InsertParagraph(headlineText, false, headLineFormat);
        doc.InsertParagraph(paraOne, false, paraFormat);

        doc.Save();

        var docFile = File(streamDoc, "application/octet-stream", "FileName.docx");
        MemoryStream streamFile = new MemoryStream();
        docFile.FileStream.Position = 0;
        docFile.FileStream.CopyTo(streamFile);

        var bites = streamFile.ToArray();
        Stream stream2 = new MemoryStream(bites);

        try
        {
            LiveOperationResult getResult = await connectedClient.UploadAsync("me/skydrive", docFile.FileDownloadName, stream2, OverwriteOption.Overwrite);
        }
        catch(WebException ex)
        {

        }
    }

    return View("~/Views/Auth/EditFile.cshtml");
}

这篇关于从C#以编程方式创建文件到Onedrive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆