Web API を使用して、アプリケーションが使用するエンドポイントを構築したいと考えています。私がやりたい最初の仕事は、クライアントがファイルをサーバーにアップロードできるようにすることです。
クライアントは、何らかの .NET アプリ (コンソール アプリなど) を実行します。フォーム要素やファイル入力を使用した Web ページにはなりません。
Web API は次のようになると思います。
public class FileController : ApiController
{
public bool Post(File newFile)
{
return true;
}
}
これをモデルクラスとして使用:
public class File
{
public string name { get; set; }
public Stream uploadStream { get; set; }
}
それはひどく間違っていると確信していますが、これは私の最初の Web API です。
コンソール アプリケーションでこれをテストしようとしています。
namespace TestFileUpload
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting the test...");
using (FileStream readstream = new FileStream(@"C:\\Test\Test2.txt", FileMode.Open, FileAccess.Read))
{
WebAPI.Classes.File newFile = new WebAPI.Classes.File()
{
name = "Test.txt",
uploadStream = readstream
};
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:50326");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
response = client.PostAsJsonAsync("http://localhost:50326/api/file", newFile).Result;
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
};
Console.ReadLine();
}
}
}
応答を取得しようとすると、タイムアウト エラーが発生します。「「System.IO.FileStream」の「ReadTimeout」から値を取得中にエラーが発生しました。
ヘルプ?