位置:海鸟网 > IT > ASP.NET >

如何在 ASP.NET 中使用 Visual Basic .NET 向 Web 服务器上载文件

WebForm1.aspx<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="VBNetUpload.WebForm1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <title>WebForm1</title> <meta content="Microsoft Visual Studio.NET 7.0"> <meta content="Visual Basic 7.0"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form enctype="multipart/form-data" method="post" runat="server"><INPUT type=file id=File1 name=File1 runat="server" ><br><input type="submit" value="Upload" runat="server"> </form> </body></HTML>
WebForm1.aspx.vbPublic Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile Protected WithEvents Submit1 As System.Web.UI.HtmlControls.HtmlInputButton#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName) Dim SaveLocation as String = Server.MapPath("Data") & "\" & fn Try File1.PostedFile.SaveAs(SaveLocation) Response.Write("The file has been uploaded.") Catch Exc As Exception Response.Write("Error: " & Exc.Message) End Try Else Response.Write("Please select a file to upload.") End If End SubEnd Class默认情况下,Machine.config 文件中的 <httpRuntime> 元素被设置为以下参数: <httpRuntimeexecutionTimeout="90"maxRequestLength="4096"useFullyQualifiedRedirectUrl="false"minFreeThreads="8"minLocalRequestFreeThreads="4"appRequestQueueLimit="100"/>