Create upload file with VB.NET
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 show you how to add Upload File to ASP.NET applications and create it in VB.NET. Through the VB.NET code example of this document, you can create a function similar to C # (if that's the language you normally use) by changing a bit in the code.
Depending on the different applications, users will have many different options on the website. Some systems such as document management system, content management system, request management system . will allow users to upload different documents.
In the example of this article, 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:
- Project Type : Visual C # Projects
- Templates : ASP.NET Web Application
- Location : http:/// localhost / FileUpload
On the web form:
1. Click Toolbox, select the HTML section, navigate to the File Field control and drag and drop it onto the form.
2. Right-click the control and set " Run as Server Control ".
3. Change the control's 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 will display as follows:
Add the following code to the form definition of the .aspx file:
encType = "multipart / form-data"
As a result, the entire form tag will look like this:
method = "post"
encType = "multipart / form-data"
runat = "server">
Add the following code to the description of the .vb file:
Dim sFileDir As String = "C:"
Dim lMaxFileSize Long = 4096
Remember that the value above will be editable depending on the application you use.You can also make them dynamic and the application will read these values from a database or from an XML file.
Add the following code to the top of the page .vb:
Imports System.IO
Add 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 it fi.Delete () End If End If End Sub
Add the following code to the .vb file:
Private Sub cmdUpload_Click (ByVal sender As System.Object, _ ByVal e As System.EventArgs)
CmdUpload.Click 'handle that file đã được chọn và nó là tập tin hợp lệ
If (Not File1.PostedFile Is Nothing) _ And (File1.PostedFile.ContentLength> 0)
Then 'determine file name Dim sFileName As String = _ System.IO.Path.GetFileName (File1.PostedFile.FileName)
Try If File1.PostedFile.ContentLength <= lMaxFileSize Then 'save file on disk File1.PostedFile.SaveAs (sFileDir + sFileName)
lblMessage.Visible = True lblMessage.Text = "File:" + sFileDir + sFileName + _ "Uploaded Successfully"
Else 'reject file lblMessage.Visible = True lblMessage.Text = "File Size if Over the Limit of" + _ lMaxFileSize
End If Catch exc As Exception 'in case của lỗi lblMessage.Visible = True lblMessage.Text = "An Error Occured. Please Try Again!" DeleteFile (sFileDir + sFileName)
End Try Else lblMessage.Visible = True lblMessage.Text = "Nothing to upload. Please Try Again!" End If End Sub
Press F5 to compile and run the project. The interface screen will look like this:
Click Browse and select a file to upload.The interface screen is as follows:
Click Upload.You will receive a message displayed above:
You should check to see if the file has been copied to the directory defined in the code.
Working
The value of sFileDir and lMaxFileSize is written in the fixed code above instead of being called 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 have 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 application. In this example, the file should not be copied to another location because you cannot call this procedure unless an error 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. It accepts a full name (directory name and file name) as an argument and needs to be verified that the file is actually existent and the length of the argument is greater than 0. Then it will try 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, you decide the file name without the directory (File1.PostedFile.FileName attribute stores the location and name of the file on the client) by using System.IO.Path.GetFileName. You will then 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 give the user a confirmation message that the file has been uploaded successfully. If an error occurs, you must delete the file and display an error 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 you try to upload a larger file, you may encounter an error message. You can change this setting by resetting maxRequestLength in the httpRuntime element of the Machine.config file.
You should read it
- How to create an EXE installation file
- Instructions to upload audio files and images to YouTube
- Instructions for creating and using BAT file on Windows
- How to upload multiple files to Dropbox from iPhone at the same time
- How to upload and upload Videos to YouTube on Android?
- Instructions for fixing errors do not download photos from iPhone to Facebook
- How to have friends upload videos to your YouTube
- How to let others upload files to your Google Drive
May be interested
- How to Create Twitch Emotesthis wikihow teaches you how to create twitch emotes using the gimp graphics editor. as long as you're a twitch affiliate or partner, you can create and upload your custom emoticons right in the twitch dashboard. install gimp from...
- How to quickly create new blank text files on Windows, Mac and Linuxtext file is useful for everything. recording a note, storing information, and journaling are just a few of the many things you can do with text files. today, we will show you how to create new blank text files in windows, mac and linux quickly. in windows, it is easy to locate new text files. but on mac and linux, this job requires some initial setup, then creating a new text file is also quick and easy.
- How to upload HD photos to Facebook on iPhone / iPad?now you can completely upload high resolution images to facbook on ios devices with just a few simple steps, no need too much time.
- How to fix errors do not upload files to Google Drivegoogle drive is definitely a reliable backup and sync solution with a range of handy features that make user work easier. however, google drive often has problems downloading files like upload speed is very slow or the file cannot be uploaded.
- How to create an ISO file on Linuxyou may know that an iso file can be burned to a cd / dvd or usb drive, but do you know that you backup or store your files and folders into an iso file? will be better? with an iso file, you can burn it to a cd / dvd as a backup or simply mount it as an external drive and access files from within.
- How to Upload, Download Videos to YouTube on iPhone?sometimes you want to upload your favorite videos from your iphone to youtube. the first way you think about it is to cut the video from your phone to your computer or laptop and upload it. however, this is quite time consuming. the following article will show you how to upload videos to youtube from your iphone device.
- Upload videos to YouTube, how to upload videos to YouTube from the fastest computerdownloading youtube videos is now much easier, along with rich video editing tools for users.
- Guide to upload HD videos to Facebook with iPhoneuploading videos to facebook is too simple, but to upload hd videos to facebook you will need some small settings. see how to upload hd videos to facebook in this article to know how to do them.
- How to Create a Computer Filethis wikihow teaches you how to create a new file, such as a document, on your computer. windows users can create basic files from within file explorer, but all computer users can create a new file from within the 'file' or 'new' menu in...
- How to create ISO file with WinRARhow to create iso file with winrar. winrar only supports viewing and extracting iso files. iso files created with winrar can only be viewed and extracted by winrar software. if you create the iso file of the boot disk, windows will have file corrupt error and we should create the iso file with specialized software such as poweriso, ultraiso ...