星期零

技术改变生活,分享让我们快乐!
随笔 - 159, 文章 - 0, 评论 - 234, 阅读 - 44万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net,C#,html控件的File控件文件上传简单实例,vs2010

Posted on   weekzero  阅读(1740)  评论(0编辑  收藏  举报

文件上传是最常用的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);
            }

        }

    }
}
复制代码

 

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2010-02-17 windows7下运行红警2(Ra2)
点击右上角即可分享
微信分享提示