zoukankan      html  css  js  c++  java
  • c# asp.net fileupload实例(10)

    显示一个文本框控件和一个浏览按钮,使用户可以选择要上载到服务器的文件。 命名空间: System.Web.UI.WebControls
    程序集: System.Web(在 system.web.dll 中)
    实例:
    html代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="inputfile._Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
            <title>无标题页</title>
    <script language="javascript" type="text/javascript">
    // <!CDATA[


    // ]]>
    </script>
            <style type="text/css">
                    .style1
                    {
                            text-align: center;
                    }
                    #form1
                    {
                            text-align: center;
                    }
            </style>
    </head>
    <body>
            <form id="form1" runat="server" method="post" enctype="multipart/form-data">
            <div style="position:static;">
                    <div class="style1">
            演示文件上传控件

                    </div>
            <hr style="80%" />

                    <div class="style1">

                    <asp:FileUpload ID="File1" runat="server"/>
                    <asp:Button ID="UploadBtn" runat="server" onclick="Button1_Click" Text="上传" />
                    </div>
            </div>
            <asp:Label ID="Label1" runat="server"
                    Width="437px" Height="61px"></asp:Label>
            </form>
    </body>
    </html>

    后台代码:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    namespace inputfile
    {
    public partial class _Default : System.Web.UI.Page
            {
    protected void Page_Load(object sender, EventArgs e)
                    {

                    }

    protected void Button1_Click(object sender, EventArgs e)
                    {//获取文件信息
    string FileName = File1.PostedFile.FileName;
    string file_str = "文件名称:" + FileName + "<br>";
                            file_str="文件类型:"+File1.PostedFile.ContentType+"<br>";
                            file_str="文件长度:"+File1.PostedFile.ContentLength.ToString()+"KB<br>";
    //上传文件到服务器
                            FileName = FileName.Substring(FileName.LastIndexOf("\\") + 1);// 取出文件名的路径(不包括文件的名称)

    string upload_file = Server.MapPath("./upload/") + FileName;//取出服务器虚拟路径,存储上传文件

                            File1.PostedFile.SaveAs(upload_file);//开始上传文件
                            Label1.Text =file_str+ "上传文件成功";
                    }
            }
    }

    常用属性:

    (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 用于设置或获取上传文件的类型
    实例效果图:

    上传完成:

    本文出自 “神舟龙” 博客,请务必保留此出处http://shenzhoulong.blog.51cto.com/1191789/30313

  • 相关阅读:
    07 oracle 非归档模式 inactive/active/current redo log损坏的恢复
    07 归档模式 Active redo log丢失或损坏的恢复
    07 oracle 归档模式 inactive/current redo log损坏修复--以及错误ORA-00600: internal error code, arguments: [2663], [0], [9710724], [0], [9711142], [], [], [], [], [], [], []
    rac的一次问题 ORA-01565: error in identifying file '+DATA/bol/spfilebol.ora'
    44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题
    43 使用分区表
    5 centos 6.10 三节点安装apache hadoop 2.9.1
    java -jar参数携带问题
    解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题
    @Autowired注解与@Qualifier注解搭配使用----解决多实现选择注入问题
  • 原文地址:https://www.cnblogs.com/shenzhoulong/p/1719620.html
Copyright © 2011-2022 走看看