zoukankan      html  css  js  c++  java
  • 步步为营-70-asp.net简单练习(文件的上传和下载)

    大文件的上传一般通过FTP协议,而一般小的文件可以通过http协议来完成

    1 通过asp.net 完成图片的上传

    1.1 创建html页面

      注意:1 method="post" ;2 enctype="multipart/form-data"; 3 <input type="file" />  

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form method="post" action="FileUpload.ashx" enctype="multipart/form-data">
            <input type="file" id="imgUpLoad" name="imgUpLoad" />
            <input type="submit" value="提交" />
        </form>
    </body>
    </html>
    FileUpload.html

    1.2 创建一般处理程序.ashx
      注意:1 创建文件保存路径

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace _05_文件的上传与下载
    {
        /// <summary>
        /// FileUpload 的摘要说明
        /// </summary>
        public class FileUpload : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
    
                //01 获取文件
                HttpPostedFile pf = context.Request.Files["imgUpLoad"];
                //02 创建文件保存路径
                string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);
                //03 保存文件
                pf.SaveAs(savePath);
                //04 显示上传的文件
                context.Response.Write("<img src='Upload/"+pf.FileName+"'/> ");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    FileUpload.ashx

    2 上传文件格式的验证,假设规定只能上传,gif的图片

      我们可以在HTML通过jQuery来进行验证,也可以在.ashx中进行验证

    2.1 修改ashx文件

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace _05_文件的上传与下载
    {
        /// <summary>
        /// FileUpload 的摘要说明
        /// </summary>
        public class FileUpload : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
    
                //01 获取文件
                HttpPostedFile pf = context.Request.Files["imgUpLoad"];
                //01-01 获取文件后缀名
                string extName = pf.FileName.Substring(pf.FileName.LastIndexOf('.'));
                if (extName != ".gif" || extName != ".Gif")
                {
                    context.Response.Write("请上传.gif图片");
                    return;
                }
                //02 创建文件保存路径
                string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload/"+pf.FileName);
                //03 保存文件
                pf.SaveAs(savePath);
                //04 显示上传的文件
                context.Response.Write("<img src='Upload/"+pf.FileName+"'/> ");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    ashx

    2.2 引入jQuery,修改HTML页面

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <script src="http://localhost:62225/Script/jquery-1.7.1.min.js"></script>
        <title></title>
        <script>
            $(function () {
                $("form").submit(function () {
                    var fname = $("#imgUpLoad").val();
                    var extname = fname.substring(fname.lastIndexOf('.'));
                    if (extname != ".gif" || extname != ".Gif") {
                        alert("请上传.gif图片");
                        return false;
                    }
                   
                });
            });
        </script>
    </head>
    <body>
        <form method="post" action="FileUpload.ashx" enctype="multipart/form-data">
            <input type="file" id="imgUpLoad" name="imgUpLoad" />
            <input type="submit" value="提交" />
        </form>
    </body>
    </html>
    html

    3 如果文件只放在Upload文件夹下,随着时间的增长,文件势必会越来越多不利于寻找,可以根据日期建立相应文件夹

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace _05_文件的上传与下载
    {
        /// <summary>
        /// FileUpload 的摘要说明
        /// </summary>
        public class FileUpload : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
    
                //01 获取文件
                HttpPostedFile pf = context.Request.Files["imgUpLoad"];
                //01-01 获取文件后缀名
                string extName = pf.FileName.Substring(pf.FileName.LastIndexOf('.'));
                if (extName != ".gif" && extName != ".GIF")
                {
                    context.Response.Write("请上传.gif图片");
                    return;
                }
                //02 创建文件保存路径
                string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Upload\");
                //02-01 根据日期创建文件夹
                DateTime dt = DateTime.Now;
                savePath += dt.Year + "\" + dt.Month + "\" + dt.Day ;
                if (!Directory.Exists(savePath))
                {
                    //创建文件夹
                    Directory.CreateDirectory(savePath);
                }
                //02-02文 件名为当前时间
                
                savePath += "\"+ dt.ToString().Replace(':','-')+".gif";
                //03 保存文件
                pf.SaveAs(savePath);
                //04 显示上传的文件
                 context.Response.Write("<img src='" + savePath.Substring(savePath.IndexOf("Upload")) + "'/> ");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    ashx

    4 文件下载

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <img src="" />
        <a href="FileDownload.ashx?f=Upload/2017.rar">Upload/2017.rar</a>
        <a href="FileDownload.ashx?f=Upload/2017-06-14%2017-25-19.gif">Upload/2017-06-14%2017-25-19.gif</a>
    </body>
    </html>
    html
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace _05_文件的上传与下载
    {
        /// <summary>
        /// FileDownload 的摘要说明
        /// </summary>
        public class FileDownload : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                string f = context.Request["f"];
                context.Response.ContentType = "application/octet-stream";
    
                context.Response.AddHeader("Content-Disposition","attachment;filename=""+f+"";");
    
                context.Response.WriteFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,f));
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    ashx

  • 相关阅读:
    [LeetCode] Recover Binary Search Tree 解题报告
    [LeetCode] Remove Nth Node From End of List 解题报告
    linux的套接口和管道
    vim配置文件管理WM和taglist
    Debian on VirtualBox下共享win7文件夹设置
    makefile文件的两种常用书写格式(搜索路径式+递归式)
    介绍一种零件分类编码系统【原】
    VSTO EXCEL篇学习笔记三【原】
    VSTO EXCEL篇学习笔记二【原】
    浅述ERP物料编码【转】
  • 原文地址:https://www.cnblogs.com/YK2012/p/7009993.html
Copyright © 2011-2022 走看看