/// <summary> /// 合并文件 /// </summary> /// <param name="path"></param> /// <returns></returns> public Tuple <bool, string, string> CombinFileMerge(string ID, string UploadFilePath, bool rename = false) { var user = SessionInfo.GetSession("GlobalConstParam.UserInfo"); if (user == null) { return(Tuple.Create(false, new ErrorMsg().GetCodeMsg("-1004"), "")); } var fileName = ResquestUtil.GetValue("fileName"); string savefilename = fileName; if (rename) { savefilename = Guid.NewGuid().ToString() + Path.GetExtension(fileName); } var path = Path.Combine(UploadFilePath, "UserInfo.GetCurrentUserInfo().accountName", Path.GetFileNameWithoutExtension(fileName));// Server.MapPath("~/App_Data/") + Path.GetFileNameWithoutExtension(fileName); //这里排序一定要正确,转成数字后排序(字符串会按1 10 11排序,默认10比2小) if (!Directory.Exists(Path.Combine(UploadFilePath, ID)))//判断是否存在此路径,如果不存在则创建 { Directory.CreateDirectory(Path.Combine(UploadFilePath, ID)); } foreach (var filePath in Directory.GetFiles(path).OrderBy(t => int.Parse(Path.GetFileNameWithoutExtension(t)))) { using (FileStream fs = new FileStream(Path.Combine(UploadFilePath, ID, savefilename), FileMode.Append, FileAccess.Write)) { byte[] bytes = System.IO.File.ReadAllBytes(filePath); //读取文件到字节数组 fs.Write(bytes, 0, bytes.Length); //写入文件 } System.IO.File.Delete(filePath); } Directory.Delete(Path.Combine(UploadFilePath, "UserInfo.GetCurrentUserInfo().accountName", Path.GetFileNameWithoutExtension(fileName)), true); return(Tuple.Create(true, fileName + "_长传成功", savefilename)); }
public Tuple <bool, string> UploadMergeFile(string UploadFilePath) { var chunk = ResquestUtil.GetValue("chunk"); //当前是第多少片 var path = Path.Combine(UploadFilePath, "UserInfo.GetCurrentUserInfo().accountName", Path.GetFileNameWithoutExtension(HttpContext.Current.Request.Files[0].FileName)); // Server.MapPath("~/App_Data/") + Path.GetFileNameWithoutExtension(Request.Files[0].FileName); if (!Directory.Exists(path)) //判断是否存在此路径,如果不存在则创建 { Directory.CreateDirectory(path); } if (HttpContext.Current.Request.Files.Count > 0 && !HttpContext.Current.Request.Form.AllKeys.Any(m => m == "chunk")) { HttpContext.Current.Request.Files[0].SaveAs(Path .Combine(path, "0")); } else { //保存文件到根目录 App_Data + 获取文件名称和格式 var filePath = path + "/" + chunk; //创建一个追加(FileMode.Append)方式的文件流 using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write)) { using (BinaryWriter bw = new BinaryWriter(fs)) { //读取文件流 BinaryReader br = new BinaryReader(HttpContext.Current.Request.Files[0].InputStream); //将文件留转成字节数组 byte[] bytes = br.ReadBytes((int)HttpContext.Current.Request.Files[0].InputStream.Length); //将字节数组追加到文件 bw.Write(bytes); } } } return(Tuple.Create(true, "文件上传成功")); }