Create file in Onedrive programmatically from C #?

I want to create a doc, docx, pptx or excel file from C # directly to my Onedrive account. I try, but it does not work for me. Does anyone know 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(); } 

Revision: The error I get is the following:

“The required parameter is not in the heading“ Content-Type ”:“ border. ”Description: An unhandled exception occurred while the current web request was running. View the stack trace for more information about the error and its occurrence in the code. Exception Details: Microsoft. Live.LiveConnectException: the header "Content-Type" does not have the required parameter: "border".

+6
source share
4 answers

Finally, I create a docx file from C #. I put the solution here (the code from the method is not reorganized, so it can be divided into harsh 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"); } 
+1
source

A few things:

  • The dictionary provided by PostAsync only fills in the fields in the request body, so adding a Content-Type there has no effect.
  • File resources must be created using the UploadAsync method, which requires content. I do not believe that there is an API that you can call to tell the service to create an empty Office document.
+2
source

I have something else. I created an HttpWebRequest, and I set some parameters. Now it creates the file in my onedrive account as docx, but when I try to open the file from my account, an error message appears and it says something like "Something went wrong. We could not open the file." The file exists, but it cannot be opened.

The code I wrote is this. Any suggestions?

 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
source

I am also responsible for creating the xlsx file.

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

Source: https://habr.com/ru/post/979910/


All Articles