Вы можете использовать приведенный ниже код для загрузки файлов любого типа с помощью веб-приложения vb.
webform1.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data" >
<div>
<asp:FileUpload ID="File1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
код webform1.aspx.vb:
Imports System.IO
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
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
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sFileDir As String = "D:\testupload\"
Dim lMaxFileSize As Long = 409600
'check that the file has been selected and it's a valid file
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 of an error
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
End Class
тестовый образец:
Примечание: вы можете увеличить размер загружаемого файла в соответствии с вашими требованиями,