问题描述
我正在尝试下载文件列表(用户可以选择多个)。我下面的当前代码在 localhost 中运行良好(编写和打开下载文件夹)。但是,当我上传到 IIS 时,它给出了一个错误,说找不到系统配置。 请参阅以下内容:
if (SelectDownloadFiles.Count > 0)
{
//Downloads folder (User profile)
string DownloadFolder = Environment.ExpandEnvironmentvariables("%userprofile%/downloads/");
//This is a little hack to get the literal path for the Downloads folder without too much of back-and-forth and ellaboration
string FolderForward = DownloadFolder.Replace(@"/",@"\");
string Folder = FolderForward.Replace(@"\\",@"\");
foreach (var items in SelectDownloadFiles)
{
//Get Date
var GetDate = items.Substring(0,6);
//Add 2 days to be consistent to what is displayed to the user (when files were generated)
var FileDate = DateTime.ParseExact(GetDate,"yyMMdd",CultureInfo.InvariantCulture).AddDays(2);
//Get Files
string Pathname = @"D:\";
string FullPathName = Path.Combine(Pathname,items);
byte[] FileBytes = System.IO.File.ReadAllBytes(FullPathName);
MemoryStream Ms = new MemoryStream(FileBytes);
//Rename the file to become user friendly
string Downloadpath = Path.Combine(DownloadFolder,"My Files " + FileDate.ToString("MM-dd-yyyy") + ".zip");
//Write file(s) to folder
FileStream File = new FileStream(Downloadpath,FileMode.Create,FileAccess.Write);
Ms.Writeto(File);
File.Close();
Ms.Close();
}
//Open Downloads Folder with files
Process.Start("explorer.exe",Folder);
navigationManager.Navigateto("/default",true);
//displayMessage.Show("File(s) successfully downloaded. Please check your “Downloads” folder to access your file(s).","OK","check");
}
else
{
Toaster.Add("Please select at least one file to download.",MatToastType.Warning);
}
我也尝试使用以下解决方案无济于事:
private readonly IWebHostEnvironment _webHostEnvironment;
public YourController (IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment= webHostEnvironment;
}
例如,如果我使用“folderoptionpath”并选择“MyDocuments”,文件将下载到 IIS 中文件的根路径。 我还需要做些什么才能让它发挥作用? 提前致谢!
解决方法
好吧,在花了一些时间研究这个之后,我终于能够开始了。我最终使用了一个名为 BlazorFileSaver 的 Nuget 包,它工作得很好。这是回购:https://github.com/IvanJosipovic/BlazorFileSaver/blob/master/src/BlazorFileSaver.Sample/Pages/Index.razor 我希望这可以在未来帮助其他人。