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
  • 相关阅读:
    ThinkPHP 3.1.2 查询方式的一般使用1
    ThinkPHP 3.1.2 查询方式的一般使用1
    php 前台数据显示
    php 前台数据显示
    CURD 例子
    CURD 例子
    ThinkPHP 3 的CURD介绍
    华为云服务器实战 之 Gitlab安装与配置使用
    【Python3网络爬虫开发实战】1.3.4-tesserocr的安装
    【Python3网络爬虫开发实战】1.3.3-pyquery的安装
  • 原文地址:https://www.cnblogs.com/0to9/p/5181414.html
Copyright © 2011-2022 走看看