My program has an asp:FileUpload control that a user will select a file with. There is a button on the page that the users can click to do some data manipulation. When that button is clicked, the asp:fileUpload control looses the file the user has chosen.
I have found code that apparently works for C# programs, but as I am coding in VB.NET, I tried converting that code.
This is the C# code I used in the converter:
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;
}
This is what the converter gave me as a VB result:
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
This is what I changed it to in order to use it for my program:
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
The code doesn't compile because of an error, in the 4th line of my code, that says: "Value of type 'FileUpload' cannot be converted to 'FileUpload'.
Is there a way I can do this in VB.NET to maintain the FileUpload file after a button click?
Edit: I took the C# code from this answer https://stackoverflow.com/a/18656681/5265207
Replace----
fileuPreCalImage = CType(Session("fileuPreCalImage"), FileUpload)
By------
fileuPreCalImage = DirectCast(Session("fileuPreCalImage"), System.Web.UI.WebControls.FileUpload)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.