Pages

Thursday, March 6, 2008

ASP.NET: Uploading Files

This is so easy. Drop a FileUpload object onto your AJAX web page. Drop in a button.

In the button event handler, do this:

//get the path to the UPLOADS directory in your webbserver
        string path=Server.MapPath(@"~\Uploads"); 

//create the full path+file name
        string SaveAsName=path+@"\"+FileUpload1.FileName;

        try
        {

//This is the bulk of it right here.
            FileUpload1.SaveAs(SaveAsName);
        }
        catch(Exception err)
        {
            Label1.Text = err.Message;
        }

That's it. The FileUpload object hands you all the data and functionality you need. Of course I could have checked to see that the file being uploaded is of a certain size or type, but this quick tutorial is just about the basics.

No comments:

Share This!

Contact Us

Name

Email *

Message *