Passing asp:FileUpload Data for Processing on Another Page
I’ve been trying to write code for a user to select a file for downloading using the FileUpload control (in ASP.NET), and then passing that information to another page that has the code to download the file from the user. After some searching, I found a Visual Basic solution that used DirectCast.
DirectCast is a type conversion operation in Visual Basic .NET which is based on inheritance or implementation. Its better to use this than CType when converting objects.
When converting VB code to C#, there is no DirectCast operation available, but standard casting can be used instead. In the following example, I’m trying to pass the contents of a FileUPload control in an ASP.NET page to another web page for processing. Its passed to the other page through a Session variable:
Session ["FileUpload"] = FileUpload1;
I found code to Visual Basic code to use the FileUpload object, but it uses DirectCast. The following shows how the code was converted to the equivalent in C#.
Dim objFileUpload As FileUpload = DirectCast(Session("FileUpload"),FileUpload)
FileUpload objFileUpload = new FileUpload ();
objFileUpload = (FileUpload) Session ["FileUpload"];
Then I use the code below to download the file, which is typical code for this control. There is no error checking in this example.
objFileUpload.SaveAs ("D:\\uploads\\" + objFileUpload.FileName);