按钮上定义的传递变量点击另一个按钮单击c#

时间:2015-11-17 13:00:44

标签: c# asp.net button directory unique

我正在编写一个ASP.NET应用程序,在按钮上单击执行这些操作

  1. 使用GUID
  2. 在App_Data文件夹中创建唯一目录
  3. 将exe文件和Excel模板复制到唯一目录
  4. 从网页输入中写一个txt文件
  5. 运行exe文件
  6. exe生成输出txt文件
  7. 将输出数据放入网页上的asp表
  8. 将输出数据从txt文件写入excel模板
  9. 点击第二个按钮

    1. 下载包含数据的唯一Excel文件
    2. 现在所涉及的所有路径都是变量。我想要做的是点击第二个按钮,它允许您下载填充数据的唯一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文件,因此我理想情况下需要再次点击按钮。

2 个答案:

答案 0 :(得分:0)

你的方法不正确,因为按钮可以按任意顺序点击,但如果你要走这条路,你可以做这样的事情。

//hide second button
yourButton2.Visible = false;

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();

    yourButton2.Visible = true;

    yourButton2.Click += (_sender, _e) =>
    {
        //excelOutput is still in scope
        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();
        }
    };
}

答案 1 :(得分:0)

谢谢@metal_man会议有所作为。好又简单。

我在定义了我独特的excel路径之后添加了这个。

    Session["excelPath"] = excelOutput;

这是我点击按钮2

    string excelOutput = (string)Session["excelPath"];

关于会话状态的好文章 http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net