C# 将excel文件保存在用户';s下载文件夹产生错误(.NET Core Razor页)
在我的razor页面应用程序中,我有一个按钮,您可以单击该按钮创建一个excel文件,并应自动将其保存到您的下载文件夹中 下面的代码在localhost中工作得很好-我单击按钮,它会保存到我的下载文件夹中,我可以查看它 但是,发布并尝试后,我收到一个错误,该错误表示“找不到路径“C:\WINDOWS\system32\config\systemprofile\Downloads\PartCommentHistory.xlsx”的一部分” 我也可以将此代码更改为打开“保存文件”对话框窗口,允许用户首先选择文件保存的位置,但我不确定如何保存。谷歌帮不了多少忙,所以我们来了 如果我实际导航到此路径,我注意到没有下载文件夹。我尝试在代码中添加一条if语句,该语句表示如果下载文件夹不存在,请先创建它,然后将文件保存在那里。但是,这会产生另一个错误,即我没有访问路径的权限 PostExportAsync(string currentFilter)上的公共异步任务 { 字符串sFilePath=Path.Combine(Environment.ExpandEnvironmentVariables(“%USERPROFILE%”),“下载”); 字符串sFileName=@“PartCommentHistory.xlsx”; stringurl=string.Format(“{0}://{1}/{2}”,Request.Scheme,Request.Host,sFileName); FileInfo file=newfileinfo(Path.Combine(sFilePath,sFileName)); var memory=newmemoryStream(); 使用(var fs=newfilestream(Path.Combine(sFilePath,sFileName),FileMode.Create,FileAccess.Write)) { ExcelPackage pck=新的ExcelPackage(); Excel工作表ws=pck.Workbook.Worksheets.Add(“工作表1”); ListCommentList=\u context.CmtPartComments.Select(x=>newcmtpartcomment { 供应商编号=x。供应商编号, 零件号=x.零件号, 注释=x.注释, EnterBy=x.EnterBy, EnteredDt=x.EnterDt.ToString(“yyyy-MM-dd HH:MM:ss tt”), CompletedDt=x.CompleteDt.ToString(“yyyy-MM-dd HH:MM:ss tt”) }).Include(c=>c.System).OrderByDescending(x=>x.EnterDt).Where(x=>x.PartNo==currentFilter).ToList(); ws.Cells[1,1].Value=“SupplierNo”; ws.Cells[1,2].Value=“PartNo”; ws.Cells[1,3].Value=“Comment”; ws.Cells[1,4].Value=“EnterBy”; ws.Cells[1,5].Value=“EnterDt”; ws.Cells[1,6].Value=“CompleteDt”; int-recordIndex=2; foreach(注释列表中的变量项) { ws.Cells[recordIndex,1].Value=item.SupplierNo; ws.Cells[recordIndex,2].Value=item.PartNo; ws.Cells[recordIndex,3].Value=item.Comment; ws.Cells[recordIndex,4].Value=item.EnterBy; ws.Cells[recordIndex,5].Value=item.enteredt; ws.Cells[recordIndex,6].Value=item.CompletedDt; recordIndex++; } ws.Cells[“A:AZ”].AutoFitColumns(); pck.SaveAs(fs); } 使用(var stream=newfilestream(Path.Combine(sFilePath,sFileName),FileMode.Open)) { 等待流。CopyToAsync(内存); } 记忆位置=0; 返回文件(内存,“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”,sFileName); }C# 将excel文件保存在用户';s下载文件夹产生错误(.NET Core Razor页),c#,asp.net-core,filestream,razor-pages,C#,Asp.net Core,Filestream,Razor Pages,在我的razor页面应用程序中,我有一个按钮,您可以单击该按钮创建一个excel文件,并应自动将其保存到您的下载文件夹中 下面的代码在localhost中工作得很好-我单击按钮,它会保存到我的下载文件夹中,我可以查看它 但是,发布并尝试后,我收到一个错误,该错误表示“找不到路径“C:\WINDOWS\system32\config\systemprofile\Downloads\PartCommentHistory.xlsx”的一部分” 我也可以将此代码更改为打开“保存文件”对话框窗口,允许用户
您无法确定文件在客户端计算机上的保存位置。它在您的机器上工作的唯一原因是您的机器充当服务器。您所能做的就是在用户下载文件时强制保存或打开对话框,这是通过将内容类型设置为
application/octet-stream
实现的:
您无法确定文件在客户端计算机上的保存位置。它在您的机器上工作的唯一原因是您的机器充当服务器。您所能做的就是在用户下载文件时强制保存或打开对话框,这是通过将内容类型设置为
application/octet-stream
实现的:
使用此方法获取文件夹路径
Environment.GetFolderPath(Environment.SpecialFolder.Yourspecialfoldernamehere, System.Environment.SpecialFolderOption.None)
比如说
Environment.GetFolderPath(Environment.SpecialFolder.System));
在上面的示例中,系统是一个特殊文件夹。使用此方法获取文件夹路径
Environment.GetFolderPath(Environment.SpecialFolder.Yourspecialfoldernamehere, System.Environment.SpecialFolderOption.None)
比如说
Environment.GetFolderPath(Environment.SpecialFolder.System));
在上面的示例中,系统是一个特殊的文件夹。对于您的问题,这是由于您在服务器端使用(var fs=new FileStream(Path.Combine(sFilePath,sFileName),FileMode.Create,FileAccess.Write))创建一个临时文件,而服务器端可能不存在该文件 对于您的需求,您正在尝试创建一个文件并将其返回到客户端。如果是这样,则无需在服务器端创建本地文件,您可以按如下方式返回文件的字节:
public async Task<IActionResult> OnPostExportByInMemoryAsync(string currentFilter)
{
string sFileName = @"PartCommentHistory.xlsx";
using (var pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Worksheet1");
ws.Cells[1, 1].Value = "SupplierNo";
ws.Cells[1, 2].Value = "PartNo";
ws.Cells[1, 3].Value = "Comment";
ws.Cells[1, 4].Value = "EnterBy";
ws.Cells[1, 5].Value = "EnterDt";
ws.Cells[1, 6].Value = "CompleteDt";
ws.Cells["A:AZ"].AutoFitColumns();
return File(pck.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName);
}
}
PostExportByInMemoryAsync(字符串currentFilter)上的公共异步任务
{
字符串sFileName=@“PartCommentHistory.xlsx”;
使用(var pck=new ExcelPackage())
{
Excel工作表ws=pck.Workbook.Worksheets.Add(“工作表1”);
ws.Cells[1,1].Value=“SupplierNo”;
ws.Cells[1,2].Value=“PartNo”;
ws.Cells[1,3].Value=“Comment”;
ws.Cells[1,4].Value=“EnterBy”;
ws.Cells[1,5].Value=“EnterDt”;
ws.Cells[1,6].Value=“CompleteDt”;
ws.Cells[“A:AZ”].AutoFitColumns();
返回文件(pck.GetAsByteArray(),“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”,sFileName);
}
}
对于您的问题,这是由于您使用(var fs=new FileStream(Path.Combine(sFilePath,sFileName),FileMode.Create,FileAccess.Write))
在服务器端创建临时文件造成的,而这可能不适用于