How can I upload a file to a Web server in ASP.NET ?
Printable View
How can I upload a file to a Web server in ASP.NET ?
use the ASP.NET button control which the the user will click to upload the file he has selected using the file control. As ASP.NET follows event based programming model we can attach a server side event which will respond to clicking of the button. This is done by using the OnClick event handler of the button control. To this event we assign the name of the method which we want to be executed on the server when the user clicks the button. (e.g)
Now we can write C# code within the UploadFile method to save the uploaded file in the server. This code can be written in two ways depending on the number of file controls we have within the page. If there is only a single file control then we can use the PostedFile property of the file control to upload files.Code:<asp:button id="cmdUpload" name="cmdUpload" runat="server" OnClick="UploadFile" />
PostedFile property is of type HttpPostedFile class and contains the following methods and properties which are used for file uploading.
Properties
- Property : Description
- FileName : Returns the full path and name of the uploaded file.
- ContentType : The MIME type of the uploaded file.
- ContentLength :The size in bytes of the uploaded file.
Method
- Methods : Description
- SaveAs(Path) : Saves the uploaded file to the specified path.
If you have multiple file controls within the form you can use the Files property of the Request object which returns a reference to the HttpFileCollection class.
You can refer to the given link for more information about this.
http://support.microsoft.com/kb/323245. :thumbup1: