File Upload with VB.NET
How This Works
The values sFileDir and lMaxFileSize have been hard coded at the top and instead can be retrieved from either a database or an XML configuration file. sFileDir specifies the location where on the server the copied file is to be saved. lMaxFileSize specifies the maximum allowed file size for an uploaded file.
The DeleteFile procedure is used to delete the copied file from the server. As a part of the normal cleanup process, after the file upload takes place, the file will be moved either to the database or to some other location on the server depending on the application requirements. In the example, you don't copy the file anywhere so you don't call this procedure unless an error occurs and you need to get rid of the file. DeleteFile can be called after the file has been moved into the database or to another location for cleaning purposes. It accepts a full file name (directory and file name) as one argument and verifies that the file exists and that the length of the argument is bigger than 0. Then, it attempts to delete the file by using the FileInfo object.
When the user clicks cmdUpload, you first check whether such a file exists. If the file exists, you determine the file name without the directory (the File1.PostedFile.FileName property stores the location and the name of the file on the client's computer) by using System.IO.Path.GetFileName. You then verify that the size of the file is not larger than the maximum allowed. After that, you save the file to the designated location on the server by using the File1.PostedFile.SaveAs method and passing the directory and the filename to it. Upon saving the file, you display a message confirming to the user that the file has been uploaded successfully. If an error occurs, you delete the file and display an error message in the lblMessage label.
Note: When uploading files, keep in mind that ASP.NET limits the file size of the uploaded files to 4 MB or 4096 KB. If you try to upload a file that is larger than that, you will get an error. You can change that setting by changing the maxRequestLength setting in the httpRuntime element of the Machine.config file. See Microsoft's web site for more information.
Conclusion
In this sample, you demonstrated a simple way of providing users with an upload document functionality by using ASP.NET and VB.NET.
About the Author
Irina Medvinskaya has been involved in technology since 1996. Throughout her career, she as developed many client/server and web applications, mainly for financial services companies. She works as a Development Manager at Citigroup.
More for Developers
On the Codeguru Forums
Visit the Forums »Featured Partner Resources
Get your Android Apps ready for Intel® Atom™ processor-based smartphones and tablets now.
Use the Android NDK to deliver the best performance on Intel® Atom™ processor-based devices.
The Android community on the Intel® Software Network has everything you need to prepare your apps for Intel® Atom™ processor-based devices.



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.