【发布时间】:2020-08-04 10:04:17
【问题描述】:
我有一个 asp.net core 3 应用程序。
我尝试从地址为 123.456.789 的服务器下载日志文件。
【问题讨论】:
标签: c# asp.net-core webclient
我有一个 asp.net core 3 应用程序。
我尝试从地址为 123.456.789 的服务器下载日志文件。
【问题讨论】:
标签: c# asp.net-core webclient
有几点需要注意:
所以,请确保您获得了存储文件的正确路径
我建议您首先创建一个小型控制台应用程序以在本地下载文件(将其保存到 C:\Temp\file.txt),一旦您将其与 webapi 集成,找到检索 AppData 的正确方法文件夹(不应在会话中)
【讨论】:
你想在这里做的是:
HttpClient 而不是WebClient。 HttpClient 在每个方面都优越
方式。HttpClient 响应写入FileStream
public async Task<IActionResult> UserLogs(string id)
{
// Set reasonable IP so HttpClient won't fail
string remoteFileUrl = "http://myuri.com/file.txt";
// Not sure what's happening here. Make sure it's pointing to reasonable
// location on machine.
// string localFileName = Path.Combine(_contextAccessor.HttpContext.Session.GetString("App_Data"), "C:\\inetpub\\logs\\LogFiles\\W3SVC15\\u_ex200621");
string localFileName = "C:\\downloads\\some_file.txt"
// Injecting via ASP.NET depenency injection is recommended however
var httpClient = new HttpClient();
var httpResponse = await httpClient.GetAsync(remoteFileUrl);
using (var fs = File.Create(localFileName))
{
httpResponse.Content.CopyToAsync(fs);
}
return View();
}
【讨论】:
https://stackoverflow.com/。基本上是 HTTP 有效的 URI