我正在尝试从 URL 读取文件,下面是代码,但出现如下错误。
代码和错误如下..
var files = new[]
{
LoadFromFile("https://teststentamappuploads.blob.core.windows.net/payrollfiles/0e21951d-e4ae-4642-ad1f-303d60cd7123/attestation132274.pdf"),
};
static InMemoryFile LoadFromFile(string path)
{
using var fs = File.OpenRead(path);
using var memFile = new MemoryStream();
fs.CopyTo(memFile);
memFile.Seek(0, SeekOrigin.Begin);
return new InMemoryFile() { Content = memFile.ToArray(), FileName = Path.GetFileName(path) };
}
错误如下:
System.IO.IOException: '文件名、目录名或卷标语法不正确。 : 'C:\用户 cs\源 epos\Zipfile\Zipfile in\Debug et6.0\https: eststentamappuploads.blob.core.windows.net\payrollfiles e21951d-e4ae-4642-ad1f-303d60cd7123 testation958874.pdf''
这不是你从网上下载文件的方式。正如 derpirscher 所提到的,
File.Open
用于访问计算机上的本地文件系统。有关如何从网络下载文件的信息,请参阅以下答案:https://stackoverflow.com/a/308336/19214431
错误表明您的路径无效,因为您无法使用
File.OpenRead()
方法从 URL 加载文件。
您应该使用
HttpClient
从 URL 获取文件。
HttpClient client = new HttpClient();
var fs = await client.GetStreamAsync(url);
using var memFile = new MemoryStream();
fs.CopyTo(memFile);
检查Make HTTP requests with the HttpClient class
错误解释:
方法
File.OpenRead()
尝试使用url作为文件名读取本地路径中的文件。 https:\\
被翻译成 https:\
这会将第二个冒号放在路径字符串中:
net6.0\https:\teststen
路径字符串无效。路径中仅在驱动器号后接受一次冒号。
C:\