Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将按钮单击时定义的变量传递给另一个按钮单击c#_C#_Asp.net_Button_Directory_Unique - Fatal编程技术网

将按钮单击时定义的变量传递给另一个按钮单击c#

将按钮单击时定义的变量传递给另一个按钮单击c#,c#,asp.net,button,directory,unique,C#,Asp.net,Button,Directory,Unique,我正在编写一个ASP.NET应用程序,只需单击按钮即可完成这些操作 使用GUID在App_数据文件夹中创建唯一目录 将exe文件和excel模板复制到唯一目录中 从网页输入中写入txt文件 运行exe文件 exe生成一个输出txt文件 将输出数据放入网页上的asp表中 将txt文件中的输出数据写入excel模板 点击第二个按钮 下载带有数据的唯一excel文件 现在所有涉及的路径都是变量。我想做的是点击第二个按钮,它允许您下载唯一的excel文件,该文件中填充了数据。但是,无论我尝试什么,我都无

我正在编写一个ASP.NET应用程序,只需单击按钮即可完成这些操作

  • 使用GUID在App_数据文件夹中创建唯一目录
  • 将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文件,因此我理想情况下需要再次单击按钮

    您的方法不正确,因为按钮可以按任何顺序单击,但如果您打算按此路线操作,您可以这样做

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

    谢谢@metal_man Session的款待。又好又简单

    我在定义了唯一的excel路径后添加了这个

        Session["excelPath"] = excelOutput;
    
    这是我的按钮

        string excelOutput = (string)Session["excelPath"];
    
    关于会话状态的好文章

    最简单的方法是使用您在btn1\u单击中设置的全局变量并进入btn2\u单击?使用
    会话[“路径”]
    ,单击第一个按钮后首先分配,单击第二个按钮后仅读取变量。使用此方法,您将仅在单击第一个按钮后为第二个按钮设置按钮单击事件。我还建议在单击第一个按钮之前隐藏第二个按钮。谢谢您的帮助。它几乎起作用了。按钮2在执行上一个代码后出现,但当我单击它时,文件不会下载。当我从Button2.click事件中取出文件时,它可以正常下载。我刚刚运行了一个测试,第二个按钮工作正常。你能用你现在的情况更新你的问题吗?可能还有别的问题。