【发布时间】:2015-11-17 13:00:44
【问题描述】:
我正在编写一个 ASP.NET 应用程序,通过单击按钮执行这些操作
- 使用 GUID 在 App_Data 文件夹中创建一个唯一目录
- 将exe文件和excel模板复制到唯一目录中
- 从网页输入写入一个 txt 文件
- 运行exe文件
- exe 生成一个输出 txt 文件
- 将输出数据放到网页上的asp表格中
- 将txt文件中的输出数据写入excel模板中
点击第二个按钮
- 下载带有数据的唯一 excel 文件
现在所有涉及的路径都是变量。我想要做的是单击第二个按钮,它允许您下载填充数据的独特 excel 文件。但是,无论我尝试什么,我都无法在第二个按钮单击中使用 excel 文件的唯一路径。
那么我如何在第二个按钮单击中传递第一个按钮单击中定义的变量。在第一次单击按钮时创建唯一目录很重要。
这里是一些代码。有很多,所以我只想发布重要的东西
public void Button1_Click(object sender, EventArgs e)
{
UniqueDirectory d = new UniqueDirectory();
string dirPath = Request.MapPath("~/App_Data/"); // point to the directory of wispp on the server
string wisppExe = Server.MapPath("~/App_Data/WisppNew.exe"); // point to the location of wispp on the server
string fortranLib = Server.MapPath("~/App_Data/libf60rts.dll"); // point to the location of the fortran libray file on the server
string excelTemplate = Server.MapPath("~/App_Data/WISPP_Template.xls");
do
{
Guid guid = Guid.NewGuid();
string uniqueSubFolderName = guid.ToString();
string uniquePath = dirPath + uniqueSubFolderName;
}
while (Directory.Exists(d.uniquePath));
Directory.CreateDirectory(d.uniquePath);
//copy files to new unique directory
string wisppUnique = Path.Combine(d.uniquePath, "WisppNew.exe");
string fortranUnique = Path.Combine(d.uniquePath, "libf60rts.dll");
string wisppGraphUnique = Path.Combine(d.uniquePath, "WISPPGRA");
string excelUnique = Path.Combine(d.uniquePath, "WISPP_Template.xls");
string excelOutput = Path.Combine(d.uniquePath, "WISPP_Output.xls");
File.Copy(wisppExe, wisppUnique, true);
File.Copy(fortranLib, fortranUnique, true);
File.Copy(excelTemplate, excelUnique, true);
//write data into excel
FileStream outputFile = new FileStream(excelOutput, FileMode.OpenOrCreate, FileAccess.Write);
xssfworkbook.Write(outputFile);
outputFile.Close();
}
public void Button2_Click(object sender, EventArgs e)
{
//here I want to use variable excelOutput
FileInfo file = new FileInfo(excelOutput);
if(file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "Application/x-msexcel";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
}
}
希望这是有道理的。我尝试将代码的响应部分放在第一个按钮单击中,但这使得数据表由于某种原因不可见。此外,我希望用户可以选择下载 excel 文件,因此理想情况下我需要单击第二个按钮。
【问题讨论】:
-
最简单的方法是使用您在 btn1_click 中设置并进入 btn2_click 的全局变量?
-
使用
Session["path"],第一个按钮被点击后首先赋值,第二个按钮被点击后读取变量。
标签: c# asp.net button directory unique