我生成了一个文本文件并保存到项目文件夹中的某个位置。如何将它们重定向到包含该文本文件的 url,以便他们可以下载文本文件。
CreateCSVFile 将 csv 文件创建到基于数据表的文件路径。
来电:
string pth = ("C:\\Work\\PG\\AI Handheld Website\\AI Handheld Website\\Reports\\Files\\report.txt");
CreateCSVFile(data, pth);
和功能:
public void CreateCSVFile(DataTable dt, string strFilePath)
{
StreamWriter sw = new StreamWriter(strFilePath, false);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
Response.WriteFile(strFilePath);
FileInfo fileInfo = new FileInfo(strFilePath);
if (fileInfo.Exists)
{
//Response.Clear();
//Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
//Response.AddHeader("Content-Length", fileInfo.Length.ToString());
//Response.ContentType = "application/octet-stream";
//Response.Flush();
//Response.TransmitFile(fileInfo.FullName);
}
}