Passing asp:FileUpload Data for Processing on Another Page

Posted by Johan Cyprich on 24 Jan 2008 | Tagged as: Programming

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;
Response.Redirect (”Upload_2.aspx”);

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#.

Visual Basic

Dim objFileUpload As FileUpload = DirectCast(Session("FileUpload"),FileUpload)

C#

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.

if (objFileUpload.HasFile)
objFileUpload.SaveAs ("D:\\uploads\\" + objFileUpload.FileName);


Related posts:
    Migrating Data from Postgres 7 to 8
    Using PHP with Mobile XHTML
    Moving the My Documents Folder
    Enabling User Instances with sp_configure

Share this post:

del.icio.us:Passing asp:FileUpload Data for Processing on Another Page digg:Passing asp:FileUpload Data for Processing on Another Page spurl:Passing asp:FileUpload Data for Processing on Another Page wists:Passing asp:FileUpload Data for Processing on Another Page simpy:Passing asp:FileUpload Data for Processing on Another Page newsvine:Passing asp:FileUpload Data for Processing on Another Page blinklist:Passing asp:FileUpload Data for Processing on Another Page furl:Passing asp:FileUpload Data for Processing on Another Page reddit:Passing asp:FileUpload Data for Processing on Another Page fark:Passing asp:FileUpload Data for Processing on Another Page blogmarks:Passing asp:FileUpload Data for Processing on Another Page Y!:Passing asp:FileUpload Data for Processing on Another Page smarking:Passing asp:FileUpload Data for Processing on Another Page magnolia:Passing asp:FileUpload Data for Processing on Another Page segnalo:Passing asp:FileUpload Data for Processing on Another Page gifttagging:Passing asp:FileUpload Data for Processing on Another Page

Follow Me:

Did you find this post interesting and useful? You can keep up to date on this blog by subscribing to my RSS feed, or you can have new posts sent to you by e-mail. You can also follow me on Twitter.


One Response to “Passing asp:FileUpload Data for Processing on Another Page”

  1. on 28 Jan 2008 at 10:45 am 1.Johan Cyprich said …

    Haissam Abdul Malak posted an article on his blog Ways to Pass Data Between Webforms.

    I’m still trying to find a better way of passing variables between a form and modules in App_Code. Passing variables through public properties is the only way I know that works now.

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply