ASP.NET File Upload Example

ASP.NET File Upload Example

Jose M. Esteban
March 28, 2001

Level: Beginner/Intermediate

Download Source Code

ASP.NET provides an easy mechanism to upload files from a Web Browser to a Web Server. The class HtmlInputFile allows to handle programmatically the information submitted by the INPUT control of the type file, e.g., <input type"file">. Remember that the Input control of type file is only available in Web Browsers that support HMTL 3.2 and later versions. This HTML version is supported by IE 3.02 and Netscape 3.0 and their later versions.

There are two objects that you will use to upload files. One is the INPUT control in the Web Browser and the other is the HtmlInputFile object in the server. The INPUT control will be embedded inside a form that will post the information encoded in a format suitable for a file transfer over the Internet. The INPUT control will also load the selected  file into the Web Browser's memory,  and will allow access to the HtmlInputFile object in the code that runs in the server through the INPUT control's ID. I.e., you use the ID of the INPUT control as the reference to the HtmlInputFile object in the code that runs at the server.

To upload a file, the form must post the data with the code format multiform/form-data instead of the default application/x-www-form-urlencoded that sends the form elements in a name/value pair base. This restriction arises from the types of formats that an object of the class HtmlInputFile is capable of processing and the characters that can traverse the Internet without confusing the network devices and software. The following example shows how to set the encoding format of a form:

<input type=file id=loFile enctype="multipart/form-data"

Once the data is submitted to the server, the object of the class HtmlInputFile exposes the property PostedFile, with an object of the type HttpPostedFile, that contains the raw file stream and the attributes of the file, such as, file length, file type, file name. But most important, the PostedFile object has the SaveAs method that allows to save the uploaded file to a local directory in the web server. You can also limit the type of the files you will allow users to upload by setting the Access property of the HtmlInputFile object, and you can also discard files base on the length by using the PostedFile.ContentLength property. The following example shows how to restrict the upload of video files. The ID loFile is the ID of the INPUT control.

loFile.Acess = "video/*" 

The example that follows shows how to enable a web form for file uploads and how to answer back to the user with a message and information about the file that was just uploaded. The web form is contained in a panel (FileUploadForm) that is initially visible and the answer is presented in a panel (AnswerMsg) that is initially disable. Once the file is uploaded, the web form panel is set to invisible and the message panel is set to visible.

The event that handles the submitted form runs at the server as indicated by the Submit INPUT tag:

<input type="submit" value="Upload" OnServerClick="UploadFile_Clicked" runat="server" ID=Submit1>

The Directory and File classes allow to create a folder to contain the uploaded files and to get the name of the uploaded file filtering the path. In order to use this classes we need to include the System.IO name space, we can do so by including the import directive. This entry allows the upload the name space into the web form application, for more information on this topic read the article How to Manipulate Files in ASP.NET:

<%@ Import Namespace="System.IO" %>

For those of you that were familiar with uploading files in ASP, the HtmlInputFile class free us from the need of third party components to process the multipart data stream.

 

Demo this code

<%@ Import Namespace="System.IO" %>
<html>
  <SCRIPT LANGUAGE="VB" RUNAT="SERVER" ID=SCRIPT1>
    Sub UploadFile_Clicked(Sender as Object, e as EventArgs)
	Dim lstrFileName as string
	Dim lstrFileNamePath as string
	Dim lstrFileFolder as string

	lstrFileFolder = "C:/TempUploadedFiles/"
	
	'Gets the file name
	lstrFileName = loFile.PostedFile.FileName
	lstrFileName = File.GetFileNameFromPath(lstrFileName)
	
	'Creates the folder if it does not exists
	If (not Directory.DirectoryExists(lstrFileFolder)) Then
		Directory.CreateDirectories(lstrFileFolder)
	End If
	
	'Save Uploaded file to server
	lstrFileNamePath = lstrFileFolder & lstrFileName
	loFile.PostedFile.SaveAs(lstrFileNamePath)
	
	'Sets the answer
	FileName.Text = lstrFileName
	FileType.Text = loFile.PostedFile.ContentType
	FileLength.Text = cStr(loFile.PostedFile.ContentLength)
	FileUploadForm.visible = false
	AnswerMsg.visible = true
    End sub
  </SCRIPT>

<BODY>

  <ASP:panel id="FileUploadForm" visible="true" runat="server">
     <form action="UploadFile.aspx" method="post" 
           enctype="multipart/form-data" 
           runat="server" ID=Form1>

	<h1>ASP.NET File Upload Example</h1>
	Select the File to Upload to the Server:<br>
	<input id="loFile" type="file" runat="server"><br>
	<input type="submit" value="Upload" 
               OnServerClick="UploadFile_Clicked" 
               runat="server" 
               ID=Submit1>

     </form>
  </ASP:panel>

  <ASP:panel id="AnswerMsg" visible="false" runat="server">
	Thanks your file <ASP:label id="FileName" runat="server" /> 
        has been uploaded<BR>
	We received <ASP:label id="FileLength" runat="server" /> 
        bytes of the type <ASP:label id="FileType" runat="server" />
   </ASP:panel>

</BODY>
</HTML>
	

 

 

 

<script language=javascript> function submitFeedback() { window.open("", "feedbackwindow", "scrolling=no,center=yes,width=200,height=150,menubar=no,resizeable=no"); document.frmFeedback.submit(); return false; } </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: ASP.NETASP.NET Core是两个不同的Web应用程序框架。ASP.NET是Microsoft开发的一种Web应用程序框架,而ASP.NET Core是ASP.NET的下一代版本。 ASP.NET是基于.NET Framework的,而ASP.NET Core是跨平台的,可以在Windows、Linux和macOS上运行。ASP.NET Core还具有更快的性能、更好的可扩展性和更好的安全性。 ASP.NET Core还提供了一种新的开发模型,即基于中间件的管道模型,这使得开发人员可以更轻松地构建和配置Web应用程序。此外,ASP.NET Core还提供了一种新的依赖注入系统,使得开发人员可以更轻松地管理应用程序中的依赖关系。 总之,ASP.NETASP.NET Core都是用于构建Web应用程序的框架,但它们之间存在一些重要的区别,包括支持的平台、性能、可扩展性和开发模型等方面。 ### 回答2: ASP.NETASP.NET Core都是Microsoft公司开发的Web应用程序框架,两者之间有很多不同之处。这篇文章将讨论它们之间的这些不同点。 1. 跨平台支持: ASP.NET是运行在Windows操作系统上的Web应用程序框架,而ASP.NET Core则是跨平台的。因此,在MacOS和Linux等其他操作系统上也可以使用ASP.NET Core。 2. 依赖的第三方库: ASP.NET依赖于大量的第三方库和框架,这些库可以添加到项目中以增强其功能。但是ASP.NET Core开发人员更多的将自己的应用程序依赖配置在库中,例如,.NET中的NuGet包。 3. 性能: 相比ASP.NETASP.NET Core更快,更高效。其中一个原因是,ASP.NET Core不需要与IIS(Internet Information Services)进行交互,这意味着更少的资源被分配, 4. 打包: ASP.NETASP.NET Core都可以使用NuGet包管理器来进行打包,但是ASP.NET Core可以将其应用程序打包为单个可执行文件,这使得开发和部署更加容易。 5. 依赖的编程语言: ASP.NET Core只能使用C#和F#等可将代码编译为.NET Core的语言,而ASP.NET则可以使用任何可编译为.NET框架的语言,包括C#,VB.NET和C++。 6. JWT的授权: 在ASP.NET Core中,JSON Web Token(JWT)是第一类公民,而在ASP.NET中,它只能使用第三方库进行实现。 7. MVC: 在ASP.NET Core中,MVC(Model-View-Controller)是默认的Web应用程序架构,但是在ASP.NET中,MVC需要安装一个独立的模板。 8. 版本: ASP.NET Core是最新的Web应用程序框架,而ASP.NET是较旧的。因此,ASP.NET Core提供了更多的功能和性能,而ASP.NET则使用固定的框架版本。 总之,虽然两者都是Microsoft公司开发的Web应用程序框架,但是它们之间还是有很多不同之处。因此,选择使用哪个框架取决于项目的要求,例如,是否需要跨平台支持和性能等。 ### 回答3: ASP.NET是一种Web应用程序框架,由Microsoft公司推出,它是Microsoft .NET运行时环境的一部分。ASP.NET提供了丰富的开发工具和框架,包括Web Forms、MVC、Web API等。它通常与IIS(Internet Information Services)一起使用,作为Web服务器上的应用程序。 ASP.NET Core是一个开源的、跨平台的Web应用程序框架,也是由Microsoft公司推出。它是Architecture Unified(一体化架构)领域的一项重要创新。ASP.NET Core是.NET平台上的一个新的、轻量级Web框架,可以跨平台运行在Windows、macOS和Linux等操作系统上。它同时支持Web Forms、MVC和Web API等多种编程模型,具有高度灵活性和可扩展性。 下面我们来详细看一下ASP.NETASP.NET Core的区别: 1.跨平台性:ASP.NET只能运行在Windows环境下,而ASP.NET Core可以运行在Windows、Linux和macOS等操作系统上。 2.开源性:ASP.NET是Microsoft公司的闭源产品,而ASP.NET Core是一个开源的多平台Web框架,所有代码都进行了公开。 3.轻量级:ASP.NET Core是一个轻量级的框架,文件大小比ASP.NET小很多,启动速度也更快。而ASP.NET则是重量级的框架,需要较高的硬件配置和更长的启动时间。 4.性能:ASP.NET Core的性能比ASP.NET更好,这是因为它是一个基于模块化设计的框架。模块化设计使得ASP.NET Core可以更容易地进行优化和扩展,而且运行时内存的消耗也更小。 5.配置简单:ASP.NET Core的配置更加简单,可以使用依赖注入模式来配置应用程序。而ASP.NET则需要在Web.config中进行大量的配置。 6.兼容性:ASP.NET Core不支持Web Forms的开发模式,而ASP.NET支持Web Forms、MVC和Web API等多种开发模式。 综上所述,ASP.NETASP.NET Core的最大区别在于跨平台性、开源性、轻量级、性能和配置的简单等方面。ASP.NET Core是一个新的、基于模块化设计的Web框架,具有更高的性能、更好的跨平台性和更简单的配置,未来将会成为ASP.NET的主要发展方向。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝈蝈俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值