zoukankan      html  css  js  c++  java
  • ASP.NET文件上传

    例子一:

    用一个正则表达式来验证上传文件的类型,去掉<asp:RegularExpressionValidator/> 能上传所有

    Default.aspx:   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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> 

    </head> 

    <body> 

        <form id="form1" runat="server"> 

            <asp:FileUpload ID="FileUpload1" runat="server" /> 

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 

          <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1" 

                ErrorMessage="必须是 jpg或者gif文件" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\W+)\$?)(\\(\W[\W].*))+(.jpg|.Jpg|.gif|.Gif)$"></asp:RegularExpressionValidator> 

        </form> 

     

    </body> 

    </html> 

     

    Default.aspx.cs:  

     

    using System;  

    using System.Data;  

    using System.Configuration;  

    using System.Web;  

    using System.Web.Security;  

    using System.Web.UI;  

    using System.Web.UI.WebControls;  

    using System.Web.UI.WebControls.WebParts;  

    using System.Web.UI.HtmlControls;  

     

    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 savePath = @"F:\Upload\";  

            if (FileUpload1.HasFile)  

            {  

                String filename;  

                filename = FileUpload1.FileName;  

                savePath +=filename;  

                FileUpload1.SaveAs(savePath);  

                Page.Response.Write(FileUpload1.PostedFile.ContentType + FileUpload1.PostedFile.ContentLength+"<br>");  

                Page.Response.Write("<img src='"+savePath+"'>");  

     

            }  

            else  

            {  

                Page.Response.Write("fff");  

            }  

        }  

    例子二:

    获得上传的文件各属性,并验证

    protected void Button1_Click(object sender, EventArgs e)  

    {  

       if (FileUpload1.HasFile)  

        {  

            fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);  

            if (fileExt == ".rar" || fileExt == ".zip")  

            {  

                try  

                {  

                    FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + FileUpload1.FileName);  

                    Label1.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "〈br>" +  

                                  "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "〈br>" +  

                                  "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "〈br>" +  

                                  "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB〈br>" +   

                                  "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "〈br>" +  

                                  "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;  

                }  

                catch (Exception ex)  

                {  

                    Label1.Text = "发生错误:" + ex.Message.ToString();  

                }  

            }  

            else  

            {  

                Label1.Text = "只允许上传rar、zip文件!";  

            }  

        }  

        else  

        {  

            Label1.Text = "没有选择要上传的文件!";  

        }  

    例子三:

    一次上传多个文件,使用HttpFileCollection类捕获从Request对象发送来的所有文件,然后再单独对每个文件进行处理

    protected void Button1_Click(object sender, EventArgs e)  

    {  

        string filepath = Server.MapPath("upload") + "\\";  

        HttpFileCollection uploadFiles = Request.Files;  

        for (int i = 0; i 〈 uploadFiles.Count; i++)  

        {  

            HttpPostedFile postedFile = uploadFiles[i];  

            try  

            {  

                if (postedFile.ContentLength > 0)  

                {  

                    Label1.Text += "文件 #" + (i + 1) + ":" + System.IO.Path.GetFileName(postedFile.FileName) + "〈br/>";  

                    postedFile.SaveAs(filepath + System.IO.Path.GetFileName(postedFile.FileName));  

                }  

            }  

            catch (Exception Ex)  

            {  

                Label1.Text += "发生错误: " + Ex.Message;  

            }  

        }  

     

     

    例子四:解决上传文件大小限制

    在ASP.NET 2.0中FileUpload默认上传文件最大为4M,不过我们可以在web.cofig中修改相关节点来更改这个默认值

    程序代码

    〈system.web> 

      〈httpRuntime maxRequestLength="40690" executionTimeout="6000" /> 

    〈/system.web> 

    maxRequestLength表示可上传文件的最大值,executionTimeout表示ASP.NET关闭前允许发生的上载秒数。

    没有目标的人都只在帮有目标的人完成目标

  • 相关阅读:
    常用API及异常
    支付宝支付流程以及二次封装alipay包 支付模块后台接口的实现 订单接口实现
    课程页前台组件 课程模块的实现 表的设计及数据录入 课程详情页前台组件 课程详情页后台实现
    celery使用
    登录、注册、页头页面 手机号验证接口 发送短信接口 注册接口 多方式登录接口 手机号登录接口 发送短信接口频率限制 异常处理修订 接口缓存
    redis基础使用 python操作redis django-redis使用
    短信接口的使用(腾讯云)
    主页设计:页面头部组件 页面底部组件 轮播图组件 主页组件 主页后台: home模块创建 路由分发 Banner表的model设计 数据库迁移 注册模块
    前台vue环境配置 vue项目的创建 项目目录的重构 全局样式文件 配置文件 axios配置 cookies配置 element-ui配置 bootstrap和jquery的配置
    thinkphp6使用最新版本composer后多应用模式提示路由不存在
  • 原文地址:https://www.cnblogs.com/tweet/p/1658100.html
Copyright © 2011-2022 走看看