Search code examples
azureamazon-web-servicesdownloadshareonedrive

How can I programmatically upload a file to my Skydrive account, getting its shareable link?


If I (manually) share a file in Onedrive/Skydrive, and use its shareable link in an HTML5 download element (this is in a ASP.NET project):

string fileUrl = "https://onedrive.live.com/redir?resid=C150662F95F0ACCE!17660&authkey=!AG7MauCst0edTWE&v=3&ithint=photo%2cpng";
string downloadElement = string.Format("<a href='{0}' download='Minimal Spreadsheet file'><button type=\"button\">Spreadsheet file</button></a>", fileUrl);
builder.Append(downloadElement);

...it works fairly well - clicking the link/button (downloadElement) doesn't download the file to my hard drive, but it does do this:

1) Downloads a file named "redir.htm"
2) When I click that file to open it, it opens the shared file on a page, from which place it can be manually downloaded by the user.

So what I would like to know is: how can I programmatically upload a file to my Skydrive account, making it shareable, and getting back the shareable URL?

I would like to be able to do something like this (in a Winforms app that will run on Windows 7):

var skyDrive = new SkyDrive(myPwd);
string shareableUrl = skyDrive.UploadFileForSharing(someFile);

// 1) Save the value in "shareableUrl" to a database table
// 2) In a REST Get method on my Web API app, read the database table to dynamically build a list of links
// 3) Using code similar to that shown at the start of this post, cause the file itself to be downloaded (rather than an htm page (IOW, I want to "cut out the middleman")).

BTW, the reason I'm gravitating toward OneDrive (over AWS, for example), at least for now (which one I ultimately stick with remains to be seen) is that the files I'm going to be saving and sharing are Excel / .xlsx files, and I reckon OneDrive will probably support their online viewing via the shareable links as good or better than anyone else (as OneDrive and Excel are both Microsoft products).

But if Onedrive doesn't make this sort of functionality/API available, I am open to using Azure or AWS insead, or just about anything else, for that matter.

UPDATE

I refactored the ASP.NET code a bit; this cuts out the "middleman" by directly opening the page with the shared file:

string sharedFileRef = string.Format("<a href='{0}' target='_blank'><button type=\"button\">Spreadsheet file</button></a>", fileUrl);
builder.Append(sharedFileRef);

...Basically, I omitted the "download" jazz, and added a "target" directive so that the shared files opens in a new page (where it is manually downloadable).


Solution

  • Did you have a look to the latest OneDrive API : https://dev.onedrive.com/README.htm ?

    There are APIs to upload (https://dev.onedrive.com/items/upload.htm) and for sharing (https://dev.onedrive.com/items/sharing.htm)

    Hope this helps