1.nuget安装BlazorDownloadFile
2.使用
@page "/" @inject HttpClient http @inject IBlazorDownloadFileService BlazorDownloadFileService <h1>Hello, world!</h1> <button @onclick="@down1">下载</button> <button @onclick="@do1">测试</button> @code{ public async void down1() { using var response = await http.GetAsync("22.txt"); using var stream = await response.Content.ReadAsStreamAsync(); await BlazorDownloadFileService.DownloadFile("33.txt", stream, "application/octet-stream"); } public async void do1() { using var response = await http.GetAsync("22.txt"); //读取txt using var stream = await response.Content.ReadAsStreamAsync(); StreamReader sr = new StreamReader(stream); var str = sr.ReadToEnd(); Console.WriteLine(str); } }