[英]Exporting a file from a specific folder for client to download
我想从客户端将下载的特定文件夹中导出文件。 我的代码如下:
string Name = UserID + "HistoricalRecords.csv";
string fileName = "C:\\Temp\\"+Name;
TextWriter textWriter = new StreamWriter(fileName);
/*Some codes which add data to the csv.*/
byte[] bytes = Encoding.ASCII.GetBytes(textWriter.ToString());
if (bytes != null)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Content-Length", bytes.Length.ToString());
HttpContext.Current.Response.AppendHeader("Content-Disposition", "Attachment; Filename=" + fileName + "");
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
在指定的文件夹中创建具有良好内容的文件。 但是,客户端下载的文件不是特定文件夹“ C:\\ Temp \\”中以数据为内容的文件。 它只是使用名称= UserID +“ HistoricalRecords.csv”创建一个没有内容的新文件。 任何想法如何解决这个问题?
您没有将创建的文件发送给客户端。 更换
HttpContext.Current.Response.BinaryWrite(bytes);
与
HttpContext.Current.Response.WriteFile(fileName);
尝试这个
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "application/CSV";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.