zoukankan      html  css  js  c++  java
  • asp.net,C#,html控件的File控件文件上传简单实例,vs2010

    文件上传是最常用的B/S项目功能,在FileUpload控件出来之前只能使用html控件的File控件,这样在form中就需要加入【 enctype="multipart/form-data"】。

    实例如下,

    up2.aspx代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="up2.aspx.cs" Inherits="up2" %>
    
    <!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>
    </head>
    <body>
        <form id="form1" runat="server" enctype="multipart/form-data">
        <input name="File" type="file"  />
        <asp:Button ID="Button1" runat="server" CssClass="button" OnClick="Button1_Click"
            Text="上传" />
        </form>
    </body>
    </html>

    up2.aspx.cs代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    using System.IO;
    
    public partial class up2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            string upPath = "/up/";  //上传文件路径
            int upLength = 5;        //上传文件大小
            string upFileExtName = "|bmp|jpg|jpeg|png|gif|";
    
            HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
    
            for (int i = 0; i < _files.Count; i++)
            {
                string name = _files[i].FileName;
    
                FileInfo fi = new FileInfo(name);
    
                string oldfilename = fi.Name;
                string scExtension = fi.Extension.ToLower();
    
                string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + fi.Extension; // 文件名称,当前时间(yyyyMMddhhmmssfff)
                string webFilePath = Server.MapPath(upPath) + fileName;        // 服务器端文件路径
    
                if (upFileExtName.IndexOf(scExtension.Replace(".", "")) == -1)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件类型不符" + scExtension + "');", true);
                    return;
                }
    
                if ((fi.Length / (1024 * 1024)) > upLength)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('大小超出 " + upLength + " M的限制,请处理后再上传!');", true);
                    return;
                }
    
                try
                {
                    _files[i].SaveAs(webFilePath);
    
                    ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件上传成功');", true);
                }
                catch (Exception ex)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件上传失败" + ex.Message + "');", true);
                }
    
            }
    
        }
    }
  • 相关阅读:
    前端学习(1)~html标签讲解(一)
    前端学习(0)~vscode工具使用
    微服务之部署
    分解单块系统
    c#之线程基础(一)
    如何在windows 7 上使用docker
    CodeForces 995B Suit and Tie(贪心,暴力)
    CodeForces 993B Open Communication(STL 模拟)
    CodeForces 993A Two Squares(数学 几何)
    CodeForces 996B World Cup(思维)
  • 原文地址:https://www.cnblogs.com/weekzero/p/2914089.html
Copyright © 2011-2022 走看看