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
  • 相关阅读:
    【洛谷】P1156 垃圾陷阱【DP】
    【洛谷】P1063 能量项链【区间DP】
    Android
    Android
    Android中使用ViewPager制作广告栏效果
    Android UI 设计之 TextView EditText 组件属性方法最详细解析
    GitHub 优秀的 Android 开源项目
    下载 编译 Android源代码 和 Android kernel源代码
    UML建模语言入门 -- 静态图详解 类图 对象图 包图 静态图建模实战
    Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
  • 原文地址:https://www.cnblogs.com/0to9/p/5181414.html
Copyright © 2011-2022 走看看