Vb .net Example to Upload a File

Create upload file with VB.NET

Uploading files is a common characteristic used by users to upload documents to server-based applications. This is an essential part of many applications from basic to more complex. In this article, I will show you lot how to add Upload File to ASP.NET applications and create it in VB.Net.

  • Upload File in PHP
  • How to create system data file in Windows 10

Uploading files is a common feature used by users to upload documents to server-based applications.This is an essential part of many applications from basic to more complex. In this article, I will evidence you how to add Upload File to ASP.NET applications and create it in VB.NET. Through the VB.NET lawmaking case of this document, you tin can create a part similar to C # (if that's the language you normally utilize) by changing a fleck in the code.

Depending on the different applications, users will have many dissimilar options on the website. Some systems such equally document management system, content management system, asking management organization . will allow users to upload dissimilar documents.

In the example of this commodity, you will have to create a simple ASP.NET web application to allow uploading a file to the server.

Open Visual Studio.Net and create a new Project with the following settings:

  1. Projection Type : Visual C # Projects
  2. Templates : ASP.Internet Web Application
  3. Location : http:/// localhost / FileUpload

On the web form:

ane. Click Toolbox, select the HTML section, navigate to the File Field command and drag and driblet it onto the grade.

2. Right-click the control and prepare " Run as Server Control ".

3. Change the control'south Name attribute to ' File1 '.

4. Go to Toolbox again, select Web Forms section, find a Button, and drag it onto the form.

5. Set Text property to ' Upload ' and ID to " cmdUpload ".

The screen volition display as follows:

Picture 1 of Create upload file with VB.NET

Add together the following code to the form definition of the .aspx file:

            encType = "multipart / course-data"          

As a result, the unabridged form tag will look like this:

            method = "post"            
encType = "multipart / form-information"
runat = "server">

Add the following code to the description of the .vb file:

            Dim sFileDir Equally String = "C:"            
Dim lMaxFileSize Long = 4096

Retrieve that the value above will be editable depending on the application you use.Yous can also make them dynamic and the application will read these values ​​from a database or from an XML file.

Add the following lawmaking to the superlative of the page .vb:

            Imports Organization.IO          

Add together the following .vb code page:

            Private Sub DeleteFile (ByVal strFileName As String) If strFileName.Trim (). Length> 0 Then Dim fi As New FileInfo (strFileName) If (fi.Exists) Then 'if file exists, delete information technology fi.Delete () Cease If End If End Sub          

Add the following code to the .vb file:

            Individual Sub cmdUpload_Click (ByVal sender As Organisation.Object, _ ByVal eastward Equally System.EventArgs)            
CmdUpload.Click 'handle that file đã được chọn và nó là tập tin hợp lệ
If (Not File1.PostedFile Is Aught) _ And (File1.PostedFile.ContentLength> 0)
Then 'determine file proper name Dim sFileName Equally String = _ System.IO.Path.GetFileName (File1.PostedFile.FileName)
Attempt If File1.PostedFile.ContentLength <= lMaxFileSize So 'save file on disk File1.PostedFile.SaveAs (sFileDir + sFileName)
lblMessage.Visible = True lblMessage.Text = "File:" + sFileDir + sFileName + _ "Uploaded Successfully"
Else 'refuse file lblMessage.Visible = True lblMessage.Text = "File Size if Over the Limit of" + _ lMaxFileSize
End If Grab exc As Exception 'in case của lỗi lblMessage.Visible = True lblMessage.Text = "An Mistake Occured. Please Try Again!" DeleteFile (sFileDir + sFileName)
End Try Else lblMessage.Visible = Truthful lblMessage.Text = "Null to upload. Please Try Again!" Stop If End Sub

Press F5 to compile and run the project. The interface screen volition look similar this:

Picture 2 of Create upload file with VB.NET

Click Scan and select a file to upload.The interface screen is as follows:

Picture 3 of Create upload file with VB.NET

Click Upload.You lot will receive a bulletin displayed to a higher place:

Picture 4 of Create upload file with VB.NET

You lot should check to come across if the file has been copied to the directory divers in the code.

Working

The value of sFileDir and lMaxFileSize is written in the fixed lawmaking above instead of being chosen from the database or XML configuration file. sFileDir specifies the location on the server so that the upload file can be saved. lMaxFileSize specifies the maximum file size for uploading.

The DeleteFile procedure is used to delete files that accept been copied to the server. As part of the normal cleanup process, after the upload file has determined the location, the file will be put into the database or somewhere on the server depending on the needs of the awarding. In this example, the file should non be copied to some other location considering you lot cannot call this procedure unless an fault occurs and need to transfer the file. DeleteFile can be called after the file has been moved to the database or to another location to intentionally remove redundant files. Information technology accepts a full name (directory proper name and file name) as an argument and needs to exist verified that the file is really existent and the length of the statement is greater than 0. Then it will endeavour to delete file using FileInfo object.

When the user clicks on cmdUpload, you must first check if the file already exists. If the file already exists, y'all decide the file name without the directory (File1.PostedFile.FileName attribute stores the location and name of the file on the client) past using Arrangement.IO.Path.GetFileName. Yous will and so have to verify that the file size is not larger than the maximum allowed capacity. Then, save the file to the location specified on the server using File1.PostedFile.SaveAs method and randomly put the directory and file name on it. When the file is saved, please requite the user a confirmation bulletin that the file has been uploaded successfully. If an error occurs, you must delete the file and brandish an fault message in the lblMessage label.

Note: When files are uploading, remember that ASP.Net limits the file size for uploading to 4MB (4096 KB). If yous try to upload a larger file, you lot may encounter an error message. Y'all can change this setting by resetting maxRequestLength in the httpRuntime chemical element of the Automobile.config file.

partlowablemody.blogspot.com

Source: https://tipsmake.com/create-upload-file-with-vbnet

0 Response to "Vb .net Example to Upload a File"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel