文件从ASP.NET网站上传到Dropbox帐户 [英] File upload from ASP.NET website to dropbox account
本文介绍了文件从ASP.NET网站上传到Dropbox帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一些文本文件从我的C#.Net Web应用程序上传到Dropbox帐户。我已经创建了Dropbox App但是没有清楚地了解如何实现这一目标。
请帮我提供从C#.Net Web应用程序上传文件到Dropbox帐户的步骤。
谢谢!
Naveen
我的尝试:
我从nuget包管理器安装了Dropbox.Api.dll和DropNet.dll。 Buut不确定如何继续进行。
Hi,
I want to upload some text files from my C# .Net web application to dropbox account. I have created dropbox App but don't getting clear idea how accomplish that.
Please help me to provide steps to upload files from C# .Net web application to dropbox account.
Thanks!
Naveen
What I have tried:
I have installed Dropbox.Api.dll and DropNet.dll from nuget Package manager. Buut not sure how to proceed further.
推荐答案
从这里抓取DropBox lib:.NET - 开发人员 - Dropbox [ ^ ]
从此处获取您的API密钥:登录 - Dropbox [ ^ ]
以下是这样的: .NET - Developers - Dropbox [ ^ ]
Grab the DropBox lib from here: .NET - Developers - Dropbox[^]
Get your API key from here: Login - Dropbox[^]
And here is how: .NET - Developers - Dropbox[^]
>
The solution to this is simple and I would highlight the procedure thus:
1.)Create an app from Dropbox App Console. Choose App Folder (this is a preferable option) or Full Dropbox for Access Type; enter a name for your app (e.g. myapp). Click "Create App" button.
Another page is displayed, click on "Generate" button under Generated Access Token.
For the rest of this write-up, the generated code will be called "token".
2.)In your Program.cs (assuming you're using Console App), ensure the following namespaces are referenced
using System;
using System.Threading.Tasks;
using Dropbox.Api;
using System.IO;
Then, modify your your code to
class Program
{
static void Main(string[] args)
{
var task = Task.Run(Upload);
task.Wait();
}
static async Task Upload()
{
var dbx = new DropboxClient("token");
var filePath = @"c:\myFile.txt"; //for example
if(File.Exists(filePath))
{
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] content;
using(var reader = new BinaryReader(fileStream))
{
content = reader.ReadBytes((int)fileStream.Length);
}
using(var mem = new MemoryStream(content))
{
await dbx.Files.UploadAsync("/myFile.txt", WriteMode.Overwrite.Instance, body:
mem);
}
}
}
}
3.) Add Newtonsoft.Json package using NuGet Package Manager and you are done.
There is a better approach to uploading bigger-sized files. But I hope this suites your scenario.
这篇关于文件从ASP.NET网站上传到Dropbox帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文