【解決方法】ASP.NET Web サイトから Dropbox アカウントへのファイルのアップロード

プログラミングQA


こんにちは、
C# .Net Web アプリケーションからいくつかのテキスト ファイルを Dropbox アカウントにアップロードしたいと考えています。 Dropbox アプリを作成しましたが、どうやってそれを実現するのかがわかりません。
C# .Net Web アプリケーションから Dropbox アカウントにファイルをアップロードする手順を教えてください。

ありがとう!
ナヴィーン

私が試したこと:

nuget パッケージ マネージャーから Dropbox.Api.dll と DropNet.dll をインストールしました。 しかし、さらに進む方法がわかりません。

解決策 1

ここから DropBox ライブラリを取得します。 .NET – 開発者 – Dropbox[^]

ここから API キーを取得します。 ログイン – Dropbox[^]

その方法は次のとおりです。 .NET – 開発者 – Dropbox[^]

解決策 2

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

C#
using System;
using System.Threading.Tasks;
using Dropbox.Api;
using System.IO;

Then, modify your your code to

C#
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.

コメント

タイトルとURLをコピーしました