如何在VB.NET中的会话中存储和检索asp:fileupload

时间:2019-06-21 13:36:31

标签: asp.net vb.net

我的程序具有一个asp:FileUpload控件,用户将使用该控件选择文件。页面上有一个按钮,用户可以单击该按钮进行一些数据操作。单击该按钮后,asp:fileUpload控件将松开用户选择的文件。

我发现显然可以用于C#程序的代码,但是当我在VB.NET中进行编码时,我尝试转换该代码。

这是我在转换器中使用的C#代码:

    if (Session["FileUpload1"] == null && FileUpload1.HasFile)
    {
        Session["FileUpload1"] = FileUpload1;
        lblFilename.Visible = true;
        lblFilename.Text = FileUpload1.FileName;
    }
    else if (Session["FileUpload1"] != null && (!FileUpload1.HasFile))
    {
        FileUpload1 = (FileUpload)Session["FileUpload1"];
        lblFilename.Visible = true;
        lblFilename.Text = FileUpload1.FileName;
    }
    else if (FileUpload1.HasFile)
    {
        Session["FileUpload1"] = FileUpload1;
        lblFilename.Visible = true;
        lblFilename.Text = FileUpload1.FileName;
    }

这就是转换器给我的VB结果:

    If Session("FileUpload1") Is Nothing AndAlso FileUpload1.HasFile Then
        Session("FileUpload1") = FileUpload1
        lblFilename.Visible = True
        lblFilename.Text = FileUpload1.FileName
    ElseIf Session("FileUpload1") IsNot Nothing AndAlso (Not FileUpload1.HasFile) Then
        FileUpload1 = CType(Session("FileUpload1"), FileUpload)
        lblFilename.Visible = True
        lblFilename.Text = FileUpload1.FileName
    ElseIf FileUpload1.HasFile Then
        Session("FileUpload1") = FileUpload1
        lblFilename.Visible = True
        lblFilename.Text = FileUpload1.FileName
    End If

为了将其用于程序,我将其更改为以下内容:

    If Session("fileuPreCalImage") Is Nothing AndAlso fileuPreCalImage.HasFile Then
        Session.Add("fileuPreCalImage", fileuPreCalImage)
    ElseIf Session("fileuPreCalImage") IsNot Nothing AndAlso (Not fileuPreCalImage.HasFile) Then
        fileuPreCalImage = CType(Session("fileuPreCalImage"), FileUpload)
    ElseIf fileuPreCalImage.HasFile Then
        Session("fileuPreCalImage") = fileuPreCalImage
    End If

代码由于错误而无法编译,在我的代码的第4行中显示:“'FileUpload'类型的值无法转换为'FileUpload'。

有没有一种方法可以在VB.NET中执行此操作以在单击按钮后维护FileUpload文件?

编辑:我从此答案https://stackoverflow.com/a/18656681/5265207中提取了C#代码

1 个答案:

答案 0 :(得分:1)

替换----

fileuPreCalImage = CType(Session("fileuPreCalImage"), FileUpload)

通过------

fileuPreCalImage =  DirectCast(Session("fileuPreCalImage"), System.Web.UI.WebControls.FileUpload)