Thursday, March 13, 2008

File upload in ASP.Net




Upload File to the Server
To upload a file to the server we need to do the foolowing steps.
1. Set the form encoding type to multipart/form-data.
<

.Drag Fileupload control from toolbox to designer window
3.Add a button called Upload to upload the File
4.Create folder in the server where the uploded files to be saved.In the example "UploadDocs" is the folder where all the uploded files will be saved.

By default, the ASPNET account under which ASP.NET runs does not have permission to write files to the filesystem of the server. You will need to modify the security settings on the folder used for uploads to allow the ASPNET user to write to the folder. The steps for doing so vary somewhat for the different flavors of Windows, but the basic steps are these:Using Windows Explorer, browse to the folder where the uploaded files will be saved.Access the security settings by right-clicking on the folder, select Properties, and then select the Security tab.Add the ASPNET user and allow write access.

Create a page called fileupload.aspx.

1.Create a File upload control name it as fileupload1

2.Create upload button

3. Change enctype to enctype="multipart/form-data" in Form tag


In onclick event of upload button,write the following code

Fileupload.aspx.vb code



****Imports the Name Space ***********


Imports System.IO




Protected Sub Upload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Upload.Click
Dim serverpath As StringDim filename As String
Dim f As FileInfofilename = FileUpload1.PostedFile.FileNamef = New FileInfo(filename)
If f.Name <> "" Then
serverpath = Server.MapPath("Uploaddocs")
FileUpload1.PostedFile.SaveAs(Server.MapPath("Uploaddocs") & "\" & f.Name)
End If
End Sub





Here ends your code...

Note : By default, file uploads are limited to 4M. Any attempt to upload a file largerthat 4M will result in an error message.
You can change the maximum file size that can be uploaded bychanging the maxRequestLength attribute of the httpRuntime element in the web.config file,

No comments:

Post a Comment