Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload Files with VB.NET

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "VB.NET how to achieve file upload", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "VB.NET how to achieve file upload" this article.

Use VB.NET to upload files to the server

VB.NET file upload technology is a very practical technology, has a wide range of applications, in the previous version of ASP.NET itself ASP to achieve this function, you must use third-party components or their own development components, now, with ASP.NET to achieve much simpler, do not need to use any components to achieve the upload function.

For ease of understanding, file uploads can be divided into two types: single file upload and multiple file upload.

Single file upload

First of all, the method of uploading a single VB.NET file is relatively simple. Here is the complete code for uploading a single file:

< %@ Import Namespace="System" %>

< %@ Import Namespace="System. Web.HttpPostedFile" %>

< %@ Import Namespace="System. Web.UI.HtmlControls.HtmlInputFile" %>

< script language="VB" runat="server">

Sub UpLoad (Src As Object, E As EventArgs)

If UploadFile.PostedFile.ContentLength=0 then

ShowUpLoadFile.innerText= "upload failed or the file does not exist!"

Else

'get the file name

Dim Temp () as String=Split

(UploadFile.PostedFile.FileName, "\")

Dim FileName as String=Temp

(Temp.Length-1)

'Save the file

UploadFile.PostedFile.SaveAs

(Server.MapPath (".") & "\ Files\

"& FileName)

'display the upload result

ShowUpLoadFile.InnerHtml=

"the file was uploaded successfully!

< br>

Upload file name: "

& FileName

End If

End Sub

< /script>

< html>

< body>

< form runat="server" enctype=" multipart/form-data">

< input type="file" id="UploadFile" runat="server" size="50">

< br>

< asp:button runat="server" Text= "立即上传" onClick="Upload" />

< /form>

< hr>

< br>

< span id="ShowUpLoadFile" runat= "server">

< /span>

< /body>

< /html>

Save the above code as an .aspx file, then create a new directory Files to store the files in the directory where the file is located, run it, feel the effect, and then continue to see the following explanation

Using VB.NET file upload, you need to use two classes of the .NET Framework: HttpPostedFile and HtmlInputFile. The namespace of these two classes is System.Web.HttpPostedFile,System.Web.UI.HtmlControls.HtmlInputFile, so we need to import these two namespaces at the beginning of the file, where PostedFile represents the file uploaded to the server, which contains several commonly used attributes:

ContentLength: file siz

FileName: detailed path and file name of the uploaded file

ContentType: the file type in which the file is uploaded.

The character splitting function Split is used to get the file name, because the detailed path and file name are obtained through PostedFile.FileName.

Multiple file upload

The so-called multi-file upload means uploading multiple files at the same time, which is mostly the same as a single file upload, but the difference is that multi-file upload uploads all files to the server as a collection of files. what we need is to decompose this set of files into single files, and the rest of the processing is the same as uploading a single file.

First of all, you need to know how many files you want to upload at the same time, and then you need to put the following HtmlInput controls between form:

< input type="file" runat= "server" size="50">

Note: the HtmlInput control here does not need to set ID, so how to retrieve files from the collection of files uploaded to the server? Look at the following code:

Dim i as integer

For iTunes 0 to Request.Files.Count-1

'use Request.Files () to do it one by one

Get uploaded files

Dim myFile as HttpPostedFile=

Request.Files (I)

'The myFile here is equivalent to the Posted in the above example

File, you can use myFile.FileName to get the file name, etc

The processing code here is the same as that uploaded by a single file

Next

The above is all the contents of the article "how to upload files in VB.NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report