转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
ASP.NET File Upload Example         ★★★★

ASP.NET File Upload Example

作者:闵涛 文章来源:闵涛的学习笔记 点击数:785 更新时间:2009/4/23 10:48:40

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>
	

 

 

 


[Delphi程序]Download a file from a FTP Server  [VB.NET程序]Code: Writing Text to a File (Visual Basic)
[Web开发]彻底放弃IIS让Apache也支持ASP.NET  [Web开发]ASP.NET 2.0发送电子邮件中存在的问题
[Web开发]ASP.NET 2.0移动开发之属性重写和模板化  [Web开发]ASP.NET 2.0中实现模板中的数据绑定
[Web开发]ASP.NET 母版页概述  [Web开发]ASP.NET 2.0 ObjectDataSource控件
[Web开发]Check if a File is in Internet Explorers Cache  [Web开发]ASP.NET 页面对象模型
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台