c# asp.net fileupload实例(10)

显示一个文本框控件和一个浏览按钮,使用户可以选择要上载到服务器的文件。 命名空间: System.Web.UI.WebControls
程序集: System.Web(在 system.web.dll 中)
实例:
html代码:
c# asp.net fileupload实例(10)<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="inputfile._Default" %>
c# asp.net fileupload实例(10)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
c# asp.net fileupload实例(10)
<html xmlns="http://www.w3.org/1999/xhtml" >
c# asp.net fileupload实例(10)<head runat="server">
c# asp.net fileupload实例(10)        <title>无标题页</title>
c# asp.net fileupload实例(10)<script language="javascript" type="text/javascript">
c# asp.net fileupload实例(10)// <!CDATA[
c# asp.net fileupload实例(10)

c# asp.net fileupload实例(10)
// ]]>
c# asp.net fileupload实例(10)</script>
c# asp.net fileupload实例(10)        <style type="text/css">
c# asp.net fileupload实例(10)                .style1
c# asp.net fileupload实例(10)                {
c# asp.net fileupload实例(10)                        text-align: center;
c# asp.net fileupload实例(10)                }
c# asp.net fileupload实例(10)                #form1
c# asp.net fileupload实例(10)                {
c# asp.net fileupload实例(10)                        text-align: center;
c# asp.net fileupload实例(10)                }
c# asp.net fileupload实例(10)        </style>
c# asp.net fileupload实例(10)</head>
c# asp.net fileupload实例(10)<body>
c# asp.net fileupload实例(10)        <form id="form1" runat="server" method="post" enctype="multipart/form-data">
c# asp.net fileupload实例(10)        <div style="position:static;">
c# asp.net fileupload实例(10)                <div class="style1">
c# asp.net fileupload实例(10)        演示文件上传控件
c# asp.net fileupload实例(10)         
c# asp.net fileupload实例(10)                </div>
c# asp.net fileupload实例(10)        <hr style="width:80%" />
c# asp.net fileupload实例(10)        
c# asp.net fileupload实例(10)                <div class="style1">
c# asp.net fileupload实例(10)        
c# asp.net fileupload实例(10)                <asp:FileUpload ID="File1" runat="server"/>
c# asp.net fileupload实例(10)                <asp:Button ID="UploadBtn" runat="server" onclick="Button1_Click" Text="上传" />
c# asp.net fileupload实例(10)                </div>
c# asp.net fileupload实例(10)        </div>
c# asp.net fileupload实例(10)        <asp:Label ID="Label1" runat="server" 
c# asp.net fileupload实例(10)                Width="437px" Height="61px"></asp:Label>
c# asp.net fileupload实例(10)        </form>
c# asp.net fileupload实例(10)</body>
c# asp.net fileupload实例(10)</html>



后台代码:
c# asp.net fileupload实例(10)using System;
c# asp.net fileupload实例(10)using System.Collections;
c# asp.net fileupload实例(10)using System.Configuration;
c# asp.net fileupload实例(10)using System.Data;
c# asp.net fileupload实例(10)using System.Linq;
c# asp.net fileupload实例(10)using System.Web;
c# asp.net fileupload实例(10)using System.Web.Security;
c# asp.net fileupload实例(10)using System.Web.UI;
c# asp.net fileupload实例(10)using System.Web.UI.HtmlControls;
c# asp.net fileupload实例(10)using System.Web.UI.WebControls;
c# asp.net fileupload实例(10)using System.Web.UI.WebControls.WebParts;
c# asp.net fileupload实例(10)using System.Xml.Linq;
c# asp.net fileupload实例(10)
namespace inputfile
c# asp.net fileupload实例(10){
c# asp.net fileupload实例(10)        public partial class _Default : System.Web.UI.Page
c# asp.net fileupload实例(10)        {
c# asp.net fileupload实例(10)                protected void Page_Load(object sender, EventArgs e)
c# asp.net fileupload实例(10)                {
c# asp.net fileupload实例(10)
                }
c# asp.net fileupload实例(10)
                protected void Button1_Click(object sender, EventArgs e)
c# asp.net fileupload实例(10)                {//获取文件信息
c# asp.net fileupload实例(10)                        string FileName = File1.PostedFile.FileName;
c# asp.net fileupload实例(10)                        string file_str = "文件名称:" + FileName + "<br>";
c# asp.net fileupload实例(10)                        file_str="文件类型:"+File1.PostedFile.ContentType+"<br>";
c# asp.net fileupload实例(10)                        file_str="文件长度:"+File1.PostedFile.ContentLength.ToString()+"KB<br>";
c# asp.net fileupload实例(10)                        //上传文件到服务器
c# asp.net fileupload实例(10)                        FileName = FileName.Substring(FileName.LastIndexOf("\\") + 1);//取出文件名的路径(不包括文件的名称)
c# asp.net fileupload实例(10)
                        string upload_file = Server.MapPath("./upload/") + FileName;//取出服务器虚拟路径,存储上传文件
c# asp.net fileupload实例(10)
                        File1.PostedFile.SaveAs(upload_file);//开始上传文件
c# asp.net fileupload实例(10)                        Label1.Text =file_str+ "上传文件成功";
c# asp.net fileupload实例(10)                }
c# asp.net fileupload实例(10)        }
c# asp.net fileupload实例(10)}

常用属性:
(1)FileUpload1.HasFile用来检查 FileUpload是否有指定文件。
(2)HttpContext.Current.Request.MapPath("~/") 则是获取网站所在的磁盘绝对路径的,如D:\Inetpub\ServerControls\路径,之所以要这么做,是因为FileUpload控件必须 指定“绝对路径”,而非相对路径,同时绝对路径也必须有写入权限。
(3)FileUpload1.SaveAs()则是将上传文件 存储在磁盘的方法。
(4)FileUpload1.FileName用于获取上传文件名称。
(5)FileUpload1.PostedFile.ContentLength 用于设置或获取上传文件大小,以Byte为单位。
   (6)FileUpload1.PostedFile.ContentType 用于设置或获取上传文件的类型
实例效果图:
c# asp.net fileupload实例(10)
上传完成:

c# asp.net fileupload实例(10)


本文转自shenzhoulong  51CTO博客,原文链接:http://blog.51cto.com/shenzhoulong/303131,如需转载请自行联系原作者

上一篇:微信小程序购物商城系统开发系列-目录结构


下一篇:goRBAC —— Go 语言基于角色的权限控制框架