Okay, so I have figured out how to take a file from OneDrive for Business and write it to a local directory on my computer with the following code:
public static async Task GetFileAsync()
{
var (authResult, message) = await Authentication.AquireTokenAsync();
var httpClient = new HttpClient();
HttpResponseMessage response;
var request = new HttpRequestMessage(HttpMethod.Get, MainPage.fileurl);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
response = await httpClient.SendAsync(request);
byte[] fileBytes = await response.Content.ReadAsByteArrayAsync();
StorageLibrary videoLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos);
string saveFolder = videoLibrary.SaveFolder.Path;
string genName = App.Generator;
genName = genName.Replace(" ", "-");
string saveFileName = App.Date + "-" + App.StartTime + "-" + App.IBX + "-" + genName + ".xlsx";
saveLocation = saveFolder + "\\" + saveFileName;
using (MemoryStream stream = new MemoryStream())
{
stream.Write(fileBytes, 0, (int)fileBytes.Length);
using (spreadsheetDoc = SpreadsheetDocument.Open(stream, true))
{
await Task.Run(() =>
{
File.WriteAllBytes(saveLocation, stream.ToArray());
return TaskStatus.RanToCompletion;
});
}
}
}
Once I saved the file on my local computer, I have been able to successfully edit the file and save the edits.
I figured out how to get it to a byte array, and how to delete the edited file in the local folder once the array is created. Now, I want to write this new edited file (byte array) back to a OneDrive subfolder.
The code I have so far is as follows:
public static async Task PutFileAsync()
{
string genName = App.Generator;
genName = genName.Replace(" ", "-");
StorageLibrary videoLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Videos);
string readFolder = videoLibrary.SaveFolder.Path;
StorageFolder videoFolder = await StorageFolder.GetFolderFromPathAsync(readFolder);
string readFileName = App.Date + "-" + App.StartTime + "-" + App.IBX + "-" + genName + ".xlsx";
StorageFile readFile = await videoFolder.GetFileAsync(readFileName);
var (authResult, message) = await Authentication.AquireTokenAsync();
var httpClient = new HttpClient();
HttpResponseMessage response;
string posturl = MainPage.spfileurl + readFile.Name + ":/content";
var content = JsonConvert.SerializeObject(readFile);
var request = new HttpRequestMessage(HttpMethod.Put, posturl);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
response = await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
await Task.Run(() =>
{
File.Delete(readFile.Path);
return TaskStatus.RanToCompletion;
});
And this last part is also tied to another post of mine.
You need to pass the json string/pure text as request body but not binary array(In your case, just read the actual content of the local file and pass the content to the json).
If you update the file content, you can use the update api
Another way: delete the old file in Drive first and upload the latest one.
Add some worked code on MVC based on your code(The key point: request.Content = new ByteArrayContent):
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
var httpClient = new HttpClient();
HttpResponseMessage response;
//string posturl = MainPage.spfileurl + readFile.Name + ":/content";
string posturl = "https://graph.microsoft.com/v1.0/me/drive/root:/Test.docx:/content";
//System.IO.FileStream file = System.IO.File.Open(@"D:\TestDocs\Allin.docx", FileMode.Open);
//string text= System.IO.File.ReadAllText(@"D:\TestDocs\Allin.docx",Encoding.Unicode);
//var content = JsonConvert.SerializeObject(file);
var request1 = new HttpRequestMessage(HttpMethod.Put, posturl);
string accessToken = await SampleAuthProvider.Instance.GetUserAccessTokenAsync();
request1.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
//request1.Content = new StringContent(content, Encoding.UTF8, "application/json");
request1.Content = new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:\TestDocs\Allin.docx"));// StringContent(text, Encoding.UTF8);
response = await httpClient.SendAsync(request1);
var responseString = await response.Content.ReadAsStringAsync();
await Task.Run(() =>
{
// System.IO.File.Delete(readFile.Path);
return TaskStatus.RanToCompletion;
});