C# Asp.net文件路径出现问题
我正在使用asp.net应用程序。我的用户必须创建一个csv文件,然后将其保存在本地或网络驱动器上。我解决这个问题的方法是在项目中创建一个文件夹,在其中创建文件,然后使用HTTP处理程序下载该文件并将其保存到其他位置。但当我重新指向HTTP处理程序时,我遇到了一个404错误,这是我的代码C# Asp.net文件路径出现问题,c#,asp.net,C#,Asp.net,我正在使用asp.net应用程序。我的用户必须创建一个csv文件,然后将其保存在本地或网络驱动器上。我解决这个问题的方法是在项目中创建一个文件夹,在其中创建文件,然后使用HTTP处理程序下载该文件并将其保存到其他位置。但当我重新指向HTTP处理程序时,我遇到了一个404错误,这是我的代码 private String serverPath = "\\PerformanceAttributionWeb\\PerfAttribution\\ExportData\\"; var fileN
private String serverPath = "\\PerformanceAttributionWeb\\PerfAttribution\\ExportData\\";
var fileName = serverPath + txtFileName.Text;
if (!File.Exists(fileName))
{
File.Create(fileName).Close();
}
Response.Redirect(string.Format("ExportData/CsvFileHandler.ashx?FileToDownload={0}", fileName));
public void ProcessRequest(HttpContext context)
{
var request = HttpContext.Current.Request;
var fileName = request.QueryString["FileToDownload"];
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + ";");
response.TransmitFile(fileName);
response.Flush();
response.End();
}
您可以在IIS中创建指向导出文件夹的虚拟目录。然后使用http url访问该文件。确保每个人都可以访问该文件夹 根本不需要处理程序。您可以直接发送文件。不需要重定向。