zoukankan      html  css  js  c++  java
  • 文件管理

    1.文件上传

    UI:

            <div class="row">
                <form id="form1" class="form-horizontal" runat="server" method="post" enctype="multipart/form-data">
                    <div>
                        <h2>文件上传</h2>
                        <input type="file" name="file"  class="input-group"/>
                        <input type="submit" name="sub" class="input-group" value="Up" />
                    </div>
                </form>
            </div>
    View Code

    root:

           protected void Page_Load(object sender, EventArgs e)
            { 
                if (Context.Request.Files.Count > 0)//如果存在文件则保存文件
                {
                    SaveFile();
                }
              } 
    
            private void SaveFile()
            {
                HttpPostedFile hpf = Request.Files["file"];  //获取文件
                FileModel fm = GetFileInfor(hpf);            //获取文件信息
                string url = "Up/" + fm.newFileName;         //获取保存路径,比如指定用户的
                hpf.SaveAs(Context.Server.MapPath(url));     //保存文件
            }
            private class FileModel
            {
                public string FileName { set; get; }
                public string FileSize { set; get; }
                public string FileUrl { set; get; }
                public string FileEnd { set; get; }
                public string newFileName { set; get; }
                public string InputStream { set; get; }
            }
            private FileModel GetFileInfor(HttpPostedFile hpf)
            {
                FileModel fm = new FileModel();
    
                string file = hpf.FileName;
                fm.FileName = System.IO.Path.GetExtension(file);
                fm.newFileName = Guid.NewGuid().ToString() + "_" + fm.FileName;
                fm.FileEnd = System.IO.Path.GetExtension(file);
                fm.FileUrl = System.IO.Path.GetFullPath(file);
                fm.FileSize = hpf.ContentLength.ToString();
    
                return fm;
            }  //获取文件信息
    View Code

    2.文件下载(只能下载有的文件,需要下载流的?)

    ui

            <div class="row">
                <div class="col-md-2"></div>
                <div class="col-md-8">
                    <h2>文件下载</h2>
                    <ul>
                        <li>
                            <a target="_blank" href="../42/File.aspx?down=bootstrap.min.css">图片下载 </a>
                        </li>
                    </ul>
                </div>
                <div class="col-md-2"></div>
            </div>
    View Code

    root

            protected void Page_Load(object sender, EventArgs e)
            { 
                if (Context.Request.QueryString["down"] != null)
                {
                    DownFile(Request["down"]);
                }
            }
            private void DownFile(string fileName)
            {
                Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                //if dilog window ,need add Header
                Response.WriteFile(fileName);
            }
    View Code
  • 相关阅读:
    大数据之MapReduce工作机制
    swoft +nginx 快速获取客户端的真实的IP
    JWT 介绍 详解
    swoft 切面编程 (AOP编程思想)
    CSP 编程模型
    Ubuntu 16.04 Mysql 重置密码
    ubuntu 提示:rm: cannot remove 'you-get/tmp': Directory not empty
    Ubuntu 系统升级到php7.2/7.3 (平滑升级)-朝花夕拾
    git clone 非标准的ssh端口(非22)端口
    Ubuntu 升级npm 以及安装cross-env 过程中遇到的问题
  • 原文地址:https://www.cnblogs.com/0to9/p/5181414.html
Copyright © 2011-2022 走看看