文件管理
1.文件上传
UI:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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>
root:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } //获取文件信息
2.文件下载(只能下载有的文件,需要下载流的?)
ui
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<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>
root
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); }