/// <summary> /// 一次性上传 /// </summary> /// <param name="request"></param> /// <returns></returns> private object UploadFile(HttpRequestBase request) { //断点续传 string app = Request["app"] ?? "epm"; string name = Request["name"]; // 是否生成压缩图片 string crop = request["crop"]; bool cropValue = "true".Equals((string.IsNullOrWhiteSpace(crop) ? "false" : crop).ToLower()); // 是否上传模型 string bim = request["isbim"]; bool isbim = "true".Equals((string.IsNullOrWhiteSpace(bim) ? "false" : bim).ToLower()); //不支持分片上传的情况处理 foreach (string file in Request.Files) { if (!string.IsNullOrEmpty(file)) { using (ClientProxyFileServer proxy = new ClientProxyFileServer(ProxyEx(Request))) { HttpPostedFileBase postedFile = Request.Files[file]; //获取客户端上载文件的集合 if (string.IsNullOrWhiteSpace(name)) { name = postedFile.FileName; } string extensionName = Path.GetExtension(name); //获取客户端上传文件的名称 //获取文件存储目录 FileConfig config = null; var configList = proxy.GetConfig(app); if (isbim) { config = configList.Data.FirstOrDefault(i => i.FileTypeDirectory == "model"); } else { config = configList.Data.FirstOrDefault(i => i.FileTypeExtension.Contains(extensionName) && i.FileTypeDirectory != "model"); } if (config == null) { return(new { flag = false, result = "不允许上传此类型的文件" }); } var pathList = CreatePath(config); string filePath = pathList[1]; string reName = Guid.NewGuid().ToString() + name; if (isbim) { reName = name; } string fullPath = filePath + "\\" + reName; postedFile.SaveAs(fullPath); //保存文件内容 FileInfo fileInfo = new FileInfo(fullPath); //正式文件入库 Files sf = new Files(); sf.App = app; sf.Day = DateTime.Now.ToString("yyyyMMdd").ToInt32Req(); sf.Extension = extensionName; sf.FilePath = fullPath; sf.FileSize = fileInfo.Length; sf.FileType = config.FileTypeName; sf.Guid = Guid.NewGuid().ToString(); sf.IsDelete = false; sf.LastModifiedDate = fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"); sf.Month = DateTime.Now.ToString("yyyyMM").ToInt32Req();; sf.Name = name; sf.ReName = reName; sf.RequetURL = Request.UrlReferrer?.ToString(); sf.UploadTime = DateTime.Now; sf.Year = DateTime.Now.Year; sf.UserDescription = ""; sf.IP = request.UserHostAddress; sf.Browser = request.Browser.Browser; string md5 = GetMD5HashFromFile(fullPath); sf.MD5 = md5; sf.VirtualURL = (filePath.Replace(config.ParentPath, "/").Replace("\\", "/") + "/" + reName); sf = proxy.AddFile(sf).Data; List <Files> list = new List <Files>(); list.Add(sf); if (ImageHelper.IsImage(extensionName) && cropValue) { string bigReName = Guid.NewGuid() + name; ImageHelper.CompressImage(fullPath, filePath + "\\" + bigReName); var virTualUrl = (filePath.Replace(config.ParentPath, "/").Replace("\\", "/") + "/" + bigReName); Files files = CreateFile(filePath, "epmbig" + name, bigReName, virTualUrl, app, extensionName, config.FileTypeName, request); files = proxy.AddFile(files).Data; list.Add(files); string smallReName = Guid.NewGuid() + name; ImageHelper.MakeThumbnailImage(fullPath, filePath + "\\" + smallReName, 300, 300, ImageHelper.ImageCutMode.Cut); var virTualUrl2 = (filePath.Replace(config.ParentPath, "/").Replace("\\", "/") + "/" + smallReName); Files smallFiles = CreateFile(filePath, "epmsmall" + name, smallReName, virTualUrl2, app, extensionName, config.FileTypeName, request); smallFiles = proxy.AddFile(smallFiles).Data; list.Add(smallFiles); } //return (new { flag = true, type = "file", file = sf }); return(new { flag = true, type = "file", file = list }); } } } return(new { flag = false, result = "文件上传失败" }); }
/// <summary> /// 分块断点续传,多文件 /// </summary> /// <param name="request"></param> /// <returns></returns> private object UploadFileByChunk(HttpRequestBase request) { //断点续传 string app = Request["app"]; string name = Request["name"]; int fId = Request["fId"].ToInt32Req(); int chunk = Request["chunk"].ToInt32Req(); //当前分块 int chunks = Request["chunks"].ToInt32Req(); //总的分块数量 // 是否上传模型 string bim = request["isbim"]; bool isbim = "true".Equals((string.IsNullOrWhiteSpace(bim) ? "false" : bim).ToLower()); foreach (string file in Request.Files) { if (!string.IsNullOrEmpty(file)) { using (ClientProxyFileServer proxy = new ClientProxyFileServer(ProxyEx(Request))) { //取得文件对比信息 FileGuid fg = proxy.GetFileGuid(fId).Data; HttpPostedFileBase postedFile = Request.Files[file]; //获取客户端上载文件的集合 string fileName = Path.GetFileNameWithoutExtension(name); //获取客户端上传文件的名称 string extensionName = Path.GetExtension(name).ToLower(); //获取客户端上传文件的后缀 string newFileName = name; //获取文件存储目录 var configList = proxy.GetConfig(app); FileConfig config = null; if (isbim) { config = configList.Data.FirstOrDefault(i => i.FileTypeDirectory == "model"); } else { config = configList.Data.FirstOrDefault(i => i.FileTypeExtension.Contains(extensionName) && i.FileTypeDirectory != "model"); } if (config == null) { config = configList.Data.FirstOrDefault(i => i.FileTypeExtension.Contains(".*")); //return (new { flag = false, result = "不允许上传此类型的文件" }); } var pathList = CreatePath(config); string tempPath = pathList[0]; string filePath = pathList[1]; //分块 if (chunks > 1) { newFileName = chunk + "_" + GetRamCode() + "_" + fileName + extensionName; //按文件块重命名块文件 } string chunkPath = tempPath + "\\" + newFileName; //将块文件和临时文件夹路径绑定 postedFile.SaveAs(chunkPath); //保存临时块上载文件内容 //临时块文件入库 FileInfo chunkFile = new FileInfo(chunkPath); TempFiles tf = new TempFiles(); tf.App = app; tf.Chunk = chunk; tf.ChunkName = newFileName; tf.ChunkPath = chunkPath; tf.Chunks = chunks; tf.ChunkSize = (int)chunkFile.Length; tf.FileGuid = Guid.NewGuid().ToString(); tf.FileLastModifiedDate = fg.LastModifiedDate; tf.FileName = name; tf.FileSize = fg.FileSize; tf.UploadTime = DateTime.Now; tf.RequetURL = Request.UrlReferrer.ToString(); tf.IP = request.UserHostAddress; tf.Browser = request.Browser.Browser; //TODO:可以在此处获取每一个块文件的md5在检测是否可以续传时返回少一个块文件的长度,然后对比续传的第一个块的md5来实现去重 tf = proxy.AddTempFile(tf).Data; //最后一个块,执行合并 if (chunks > 1 && chunk + 1 == chunks) { var tempFiles = proxy.GetTempFiles(app, name, fg.LastModifiedDate, fg.FileSize); var fileList = tempFiles.Data.OrderBy(i => i.Chunk); string reName = Guid.NewGuid().ToString() + name; string fullPath = filePath + "\\" + reName; using (FileStream fsw = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) { BinaryWriter bw = new BinaryWriter(fsw); // 遍历文件合并 foreach (var temp in fileList) { //打开一个文件读取流信息,将其写入新文件 bw.Write(System.IO.File.ReadAllBytes(temp.ChunkPath)); System.IO.File.Delete(temp.ChunkPath); //删除临时块文件信息,以避免临时文件越来越大 bw.Flush(); //清理缓冲区 } } //正式文件入库 Files sf = new Files(); sf.App = app; sf.Day = DateTime.Now.ToString("yyyyMMdd").ToInt32Req();; sf.Extension = extensionName; sf.FilePath = fullPath; sf.FileSize = fg.FileSize; sf.FileType = config.FileTypeName; sf.Guid = fg.FId; sf.IsDelete = false; sf.LastModifiedDate = fg.LastModifiedDate; sf.Month = DateTime.Now.ToString("yyyyMM").ToInt32Req();; sf.Name = name; sf.ReName = reName; sf.RequetURL = Request.UrlReferrer.ToString(); sf.UploadTime = DateTime.Now; sf.Year = DateTime.Now.Year; sf.UserDescription = ""; sf.IP = request.UserHostAddress; sf.Browser = request.Browser.Browser; string md5 = GetMD5HashFromFile(fullPath); sf.MD5 = md5; sf.VirtualURL = (filePath.Replace(config.ParentPath, "/").Replace("\\", "/") + "/" + reName); sf = proxy.AddFile(sf).Data; //删除临时块文件信息 proxy.DeleteTempFile(fileList.Select(i => i.Id).ToList()); //删除fileguid对比信息 proxy.DeleteFileGuid(fg); return(new { flag = true, type = "file", file = sf }); } return(new { flag = true, type = "chunk", chunk = tf }); } } } return(new { flag = false, result = "文件上传失败" }); }