Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, April 18, 2020 6:58 AM
Hello Everyone,
I'm stuck in uploading the files to MS OneDrive from C# Console application,
I have used the Graphical API but couldn't complete getting access token Error,can anyone give some suggestions
All replies (2)
Saturday, April 18, 2020 1:06 PM
Maybe this can help you out:
Using the code:
public string OneDriveApiRoot { get; set; } = "https://api.onedrive.com/v1.0/";
Upload file to OneDrive
//this is main method of upload file to OneDrive
public async Task<string> UploadFileAsync(string filePath, string oneDrivePath)
{
//get the upload session,we can use this session to upload file resume from break point
string uploadUri = await GetUploadSession(oneDrivePath);
//when file upload is not finish, the result is upload progress,
//When file upload is finish, the result is the file information.
string result = string.Empty;
using (FileStream stream = File.OpenRead(filePath))
{
long position = 0;
long totalLength = stream.Length;
int length = 10 * 1024 * 1024;
//In one time, we just upload a part of file
//When all file part is uploaded, break out in this loop
while (true)
{
//Read a file part
byte[] bytes = await ReadFileFragmentAsync(stream, position, length);
//check if arrive file end, when yes, break out with this loop
if (position >= totalLength)
{
break;
}
//Upload the file part
result = await UploadFileFragmentAsync(bytes, uploadUri, position, totalLength);
position += bytes.Length;
}
}
return result;
Monday, April 20, 2020 5:41 AM
Hi Jagadeesha Ravibabu,
I viewed your description, what graphical API you used? Could you provide some related code to reproduce the situation?
And here are some threads about uploading the files to the Onedrive you can refer to.
[C# login and upload to OneDrive without user interaction]
[How to upload file to OneDrive through console app?]
[30DaysMSGraph – Day 29 – Use case: Upload files to OneDrive]
Best Regards,
Daniel Zhang
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].