I making MVC website. I would like to use dropbox for storing my files. I find some code hove to upload files but I have no idea how to return back file url. How to upload file to dropbox and return uploaded file url to save in db?
My code:
async Task Upload(DropboxClient dbx, string folder, string file, string content)
{
using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
var updated = await dbx.Files.UploadAsync(
folder + "/" + file,
WriteMode.Overwrite.Instance,
body: mem);
Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
}
}
How to upload file to dropbox and return uploaded file url to save in db?
Well, within upload file you would see Dropbox.Api.Sharing.CreateSharedLinkWithSettingsArg method which has the response type of SharedLinkMetadata class just like as following:
As you can see SharedLinkMetadata class has the property name URL which is :
public string Url { get; protected set; }
Above property would provide you filesharing URL metadata. Thus, you can update your Upload method as following:
public static async Task<string> UploadFile(string folder, string fileName, string fileUri)
{
var dropBoxClient = new DropboxClient("Token","AppKey");
using (var ms = new FileStream(fileUri, FileMode.Open, FileAccess.Read))
{
FileMetadata updated = await dropBoxClient.Files.UploadAsync(
folder + "/" + fileName,
WriteMode.Overwrite.Instance,
body: ms);
var shareLinkInfo = new Dropbox.Api.Sharing.CreateSharedLinkWithSettingsArg(folder + "/" + fileName);
var reponseShare = await dropBoxClient.Sharing.CreateSharedLinkWithSettingsAsync(shareLinkInfo);
return reponseShare.Url;
}
}
Note: If you still need further information on this, please have a look on dropbox official document here.