2014-12-19 120 views
6

我想从C#直接创建一个doc,docx,pptx或excel文件到我的Onedrive帐户。 我试过这个,但它不适合我。任何人都知道我做错了什么?由于从C#编程创建文件到Onedrive?

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

编辑: ,我得到的错误是这一个:

“头‘的Content-Type’缺少必要的参数:‘边界’ 说明:发生未处理的异常在执行当前Web请求期间请检查堆栈跟踪以获取有关该错误的更多信息以及源代码的位置 异常详细信息:Microsoft.Live.LiveConnectException:头文件'Content-Type'缺少所需的参数:'边界'。“

+5

错误消息,什么不工作? – 2014-12-19 11:02:12

+0

确保您已更新为使用Live SDK 5.6二进制文件。 – sagar43 2014-12-19 11:13:49

+0

我在帖子中输入了我得到的错误信息,并且已经有5.6更新(这不是因为Live SDK的版本) – CalinCosmin 2014-12-19 11:21:39

回答

1

最后我从c#创建一个docx文件。我把解决方案放在这里(方法中的代码不是重构的,因此可以用严格的方法分割)。

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"); 
} 
+0

我如何获得Microsoft.Live dll? – 2017-02-16 12:05:25

+0

通过nuget包管理器控制台。检查那里,你会发现OneDrive SDK – CalinCosmin 2017-06-12 12:35:45

+0

我在Fab工作。 btw thnx重播:) – 2017-06-13 13:07:37

2

几件事情:

  1. 提供给PostAsync字典只填充字段请求主体,并因此增加的Content-Type在那里没有任何效果
  2. 文件资源平均将使用需要内容的UploadAsync方法创建。我不相信有一个API可以调用来告诉服务创建一个空白的Office文档。
+0

我成功地使用UploadAsync()在onedrive上传word,excel和许多其他文档。这里是我如何调用方法的一个例子:“LiveOperationResult uploadResult = await connectedClient.UploadAsync(folderId,fileName,fileStream,OverwriteOption.Overwrite);”其中folderId =“me/skydrive”,fileName =“Document.docx”,filestream =我从计算机中选取的文件的inputStream,它在onedrive中创建了doc文件。我在“fileStream”中自动设置了contentType,所以我认为这应该是一种手动设置它的方法。 – CalinCosmin 2014-12-20 14:42:10

1

我还有别的东西。我创建了一个HttpWebRequest并设置了一些参数。现在它将文件作为docx创建到我的onedrive帐户,但是当我尝试从我的帐户打开文件时,出现一条错误消息,它说“发生了什么问题,我们无法打开文件”。该文件存在但无法打开。

我写的代码是这样的。有什么建议么 ?

public async Task<ActionResult> CreateWordFile() 
{ 
    string body = "--A300x\r\n" 
      + "Content-Disposition: form-data; name=\"file\"; filename=\"csm.docx\"\r\n" 
      + "Content-Type: application/octet-stream\r\n" 
      + "\r\n" 
      + "This is some content\r\n" 
      + "\r\n" 
      + "--A300x--\r\n"; 

    byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(body); 
    Stream stream = new MemoryStream(fileBytes); 

    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; 


     HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url); 
     httpWebRequest2.ContentType = "multipart/form-data; boundary=A300x"; 
     httpWebRequest2.Method = "POST"; 
     httpWebRequest2.KeepAlive = true; 
     httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials; 
     httpWebRequest2.ContentLength = fileBytes.Length; 
     Stream stream2 = httpWebRequest2.GetRequestStream(); 
     stream2.Write(fileBytes, 0, fileBytes.Length); 
     WebResponse webResponse2 = httpWebRequest2.GetResponse(); 
     } 

    return View(); 
} 
1

我也找到了创建xlsx文件的答案。

public async Task<ActionResult> CreateExcelFile() 
     { 
      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; 

       XSSFWorkbook wb = new XSSFWorkbook(); 

       // create sheet 
       XSSFSheet sh = (XSSFSheet)wb.CreateSheet("Sheet1"); 
       // 10 rows, 10 columns 
       for (int i = 0; i < 100; i++) 
       { 
        var r = sh.CreateRow(i); 
        for (int j = 0; j < 100; j++) 
        { 
         r.CreateCell(j); 
        } 
       } 

       MemoryStream stream = new MemoryStream(); 
       wb.Write(stream); 
       stream.Dispose(); 

       var arrBites = stream.ToArray(); 

       MemoryStream newStream = new MemoryStream(arrBites); 

       var docFile = File(newStream, "application/octet-stream", "Excel.xlsx"); 
       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(); 
     }