/// <summary> /// 合并文件 /// </summary> /// <param name="fileName"></param> /// <param name="ext"></param> /// <returns></returns> public JsonResult MergeFiles(string oldName, string tempDirectory) { string ext = System.IO.Path.GetExtension(oldName); string filePath = string.Format(@"{0}/UploadFiles/{1}{2}", _hostingEnvironment.WebRootPath, Guid.NewGuid().ToString("n"), ext); string directory = string.Format(@"{0}/UploadFiles/{1}/", _hostingEnvironment.WebRootPath, tempDirectory); var fileList = System.IO.Directory.GetFiles(directory) // 按照文件名(数字)排序 .OrderBy(s => int.Parse(System.IO.Path.GetFileNameWithoutExtension(s))) .ToList(); foreach (string itempath in fileList) { using (var stream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) { // System.Threading.Thread.Sleep(1000); byte[] bytes = System.IO.File.ReadAllBytes(itempath); stream.Write(bytes, 0, bytes.Length); } System.IO.File.Delete(itempath); } Directory.Delete(directory); FileInfo fielinfo = new System.IO.FileInfo(filePath); DtFile entity = new DtFile(); entity.FileExt = ext; entity.Length = (int)fielinfo.Length; entity.NewName = fielinfo.Name; entity.OldName = oldName; entity.Path = @"/UploadFiles/" + fielinfo.Name; entity.Type = 4; var extlist = FIleTypeServer.GetFileTypeList(); foreach (var item in extlist) { if (item.ExtList.Contains(ext.ToLower())) { entity.Type = item.Type; break; } } _fileMgmt.Inserter(entity); _fileMgmt.Save(); return(Json(new { flag = true })); }
public JsonResult UploadFile(List <IFormFile> files) { string name = Guid.NewGuid().ToString("n"); string filePath = _hostingEnvironment.WebRootPath + @"/UploadFiles/"; foreach (var formFile in files) { string ext = System.IO.Path.GetExtension(formFile.FileName); System.IO.Directory.CreateDirectory(filePath); if (formFile.Length > 0) { string path = filePath + name + ext; using (var stream = new FileStream(path, FileMode.Create)) { formFile.CopyToAsync(stream); DtFile entity = new DtFile(); entity.FileExt = ext; entity.Length = (Int32)formFile.Length; entity.NewName = name; entity.OldName = formFile.FileName; entity.Path = @"/UploadFiles/" + name + ext; entity.Type = 4; var extlist = FIleTypeServer.GetFileTypeList(); foreach (var item in extlist) { if (item.ExtList.Contains(ext.ToLower())) { entity.Type = item.Type; break; } } _fileMgmt.Inserter(entity); _fileMgmt.Save(); } } } return(Json(new { flag = true })); }