/// <summary> /// 上传文件 /// </summary> /// <returns></returns> [HttpPost] public ActionResult Upload() { //调用页面的上传控件,实现文件的上传 var file = Request.Files[0]; var id = Request["id"] ?? ""; //如果是重新上传的话,会有id var type = Request["type"] ?? ""; //主文件没有type,照片卷的照片标签、图纸卷的图纸备考表、拓片及摹本的登记表、电子文件卷的光盘标签,这几类辅助文件会有type if ((file != null) && (file.ContentLength > 0)) { string uploadfilename = file.FileName; //用户上传的原始文件名,数据库中也要存储,用于用户下载的时候重命名 string extention = uploadfilename.Substring(uploadfilename.LastIndexOf(".")).ToLower(); string saveFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + extention; string path = Server.MapPath("~/Upload/SYZL/Doc/");//取得上传的目标文件夹 string filePath = path + saveFileName; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } file.SaveAs(Path.Combine(path, saveFileName)); //上传文件 if ((extention == ".pdf") || (extention == ".doc") || (extention == ".docx") || (extention == ".xls") || (extention == ".xlsx") || (extention == ".ppt") || (extention == ".pptx")) { //在完成pdf,office格式文档上传之后,进行文档格式的转换,以便使用flexpaper在线预览 ConvertFileFormat(saveFileName); /* var workingThread = new Thread(ConvertFileFormat); workingThread.Start(saveFileName); */ } //更新数据库记录,新增数据库记录的操作不在这里做,在提交表单的时候做,原始文件名和保存文件名已经返回给表单 if (id != "") //id不为空,是重新上传文件动作,需要更新主键为id的数据库记录 { var syxxxq = db.T_SYXXXQ.Find(Int32.Parse(id)); if (syxxxq != null) { //判断type是否为空,如果type为空,则为主文件,否则,则为辅文件 if (type != "") { var ajdhqlw = syxxxq.SYXXZLLXBH.Trim(); //案卷档号前两位 //如果老文件存在,则删除老文件 DeleteOldFile(syxxxq.LJWJMC); //更新图纸卷的"图纸备考表原始上传文件名":TZBKBYSM,"图纸备考表保存文件名":TZBKBBCM syxxxq.NRBT = uploadfilename; //原始文件名 syxxxq.LJWJMC = saveFileName; //保存文件名 db.Entry(syxxxq).State = EntityState.Modified; db.SaveChanges(); } else { //如果老文件存在,则删除老文件 DeleteOldFile(syxxxq.NRBT); //上传的是主文件,更新主文件数据库记录 syxxxq.NRBT = uploadfilename; //原始文件名 syxxxq.LJWJMC = saveFileName; //保存文件名 syxxxq.WJLX = GetFileTypeByExtention(extention); syxxxq.ADDSJ = DateTime.Now; db.Entry(syxxxq).State = EntityState.Modified; db.SaveChanges(); } } } //返回保存文件名,原始文件名 return Content("{\"NRBT\":\"" + uploadfilename + "\",\"LJWJMC\":\"" + saveFileName + "\",\"WJLJ\":\"" + filePath + "\",\"WJLX\":\"" + GetFileTypeByExtention(extention) + "\"}"); } return Content("0"); }
上传
1 public string downloadFile() 2 { 3 string id = Request["id"] ?? ""; 4 if (!string.IsNullOrEmpty(id)) 5 { 6 var syxxxq = db.T_SYXXXQ.Find(Int32.Parse(id)); 7 string path = Server.MapPath("~/Upload/SYZL/Doc/");//取得上传的目标文件夹 8 string fileName = syxxxq.LJWJMC;//保存的路径文件名 9 string khdFileName = syxxxq.NRBT;//客户端文件名 10 string filePath = path + fileName;//服务器上保存的路径 11 var fileInfo = new FileInfo(filePath); 12 if (fileInfo.Exists == true) 13 { 14 #region 未测试的另一种下载方法 15 //FileInfo fi = new FileInfo(fileName); 16 //Response.Clear(); 17 //Response.ClearHeaders(); 18 //Response.Buffer = true; 19 //Response.Charset = "GB2312"; 20 21 ////添加头信息,为 "文件下载/另存为 "对话框指定默认文件名 22 //Response.AppendHeader("Content-Disposition", "attachment;filename=" 23 // + HttpUtility.UrlEncode(Path.GetFileName(filePath), 24 // System.Text.Encoding.UTF8)); 25 //Response.AppendHeader("Content-Length", fi.Length.ToString()); 26 //Response.ContentType = "application/octet-stream"; 27 //Response.Filter.Close(); 28 //Response.WriteFile(filePath); 29 //Response.Flush(); 30 //Response.End(); 31 #endregion 32 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 33 byte[] buffer = new byte[ChunkSize]; 34 35 Response.Clear(); 36 var iStream = System.IO.File.OpenRead(filePath); 37 long dataLengthToRead = iStream.Length;//获取下载的文件总大小 38 Response.ContentType = "application/octet-stream"; 39 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); 40 while (dataLengthToRead > 0 && Response.IsClientConnected) 41 { 42 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 43 Response.OutputStream.Write(buffer, 0, lengthRead); 44 Response.Flush(); 45 dataLengthToRead = dataLengthToRead - lengthRead; 46 } 47 Response.Close(); 48 } 49 } 50 return "下载的文件不存在!"; 51 }
下载
思路、参考。
时间: 2024-10-12 08:27:35