public async Task <HttpReponseViewModel <GenerateMD5ToBurstResViewModel> > GetMD5ToBurstData(Guid id) { HttpReponseViewModel <GenerateMD5ToBurstResViewModel> res = new HttpReponseViewModel <GenerateMD5ToBurstResViewModel>(); var t = await _baseDomain.GetEntity(id); if (t != null) { var filePath = t.RelationFilePath; var fileName = t.FileName + "." + t.FileExt; var len = FileUtil.GetLength(filePath); res.Data = new GenerateMD5ToBurstResViewModel() { Identifier = FileUploadUtil.GetMD5HashFromFile(filePath), TotalSize = len, FilePathUrl = filePath, FileName = fileName, FileRanges = GetFileRangeData(len) }; } else { res.Data = null; res.Message = "该文件不存在"; } res.Code = 200; res.ResultSign = Core.Enum.ResultSign.Info; return(res); }
public async Task <HttpReponseViewModel <List <TView> > > GetPageListService(QueryModel queryModel) { var pageData = await _baseDomain.GetPageList(queryModel); var list = pageData.DataList.MapToList <TView>(); HttpReponseViewModel <List <TView> > httpReponse = new HttpReponseViewModel <List <TView> >(list, queryModel.PageIndex, queryModel.PageSize, pageData.Total, pageData.EXESql); return(httpReponse); }
// [Transactional] public async Task <HttpReponseViewModel <TestViewModel> > AddOrEditService1(TestViewModel entity) { HttpReponseViewModel <TestViewModel> res = new HttpReponseViewModel <TestViewModel>(); var t1 = entity.MapTo <Test>(); var flag = await _userDomain.AddDomain(t1); //var t2 = entity.MapTo<Test>(); //var flag1 = await _userDomain.AddDomain(t1); return(res); }
public async Task <HttpReponseViewModel <string> > GetDownloadSmallFiles(Guid id, HttpContext httpContext) { HttpReponseViewModel <string> res = new HttpReponseViewModel <string>(); var t = await _baseDomain.GetEntity(id); if (t != null) { var filePath = t.RelationFilePath; var response = httpContext.Response; var fileName = Path.GetFileName(filePath); //测试文档.xlsx int bufferSize = 10240 * 80; //这就是ASP.NET Core循环读取下载文件的缓存大小,这里我们设置为了1024字节,也就是说ASP.NET Core每次会从下载文件中读取1024字节的内容到服务器内存中,然后发送到客户端浏览器,这样避免了一次将整个下载文件都加载到服务器内存中,导致服务器崩溃 response.ContentType = MimeMappingUtil.GetMimeMapping(filePath); //由于我们下载的是一个Excel文件,所以设置ContentType为application/vnd.ms-excel var contentDisposition = "attachment;" + "filename=" + HttpUtility.UrlEncode(fileName); //在Response的Header中设置下载文件的文件名,这样客户端浏览器才能正确显示下载的文件名,注意这里要用HttpUtility.UrlEncode编码文件名,否则有些浏览器可能会显示乱码文件名 response.Headers.Add("Content-Disposition", new string[] { contentDisposition }); //使用FileStream开始循环读取要下载文件的内容 using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (response.Body) //调用Response.Body.Dispose()并不会关闭客户端浏览器到ASP.NET Core服务器的连接,之后还可以继续往Response.Body中写入数据 { long contentLength = fs.Length; //获取下载文件的大小 response.ContentLength = contentLength; //在Response的Header中设置下载文件的大小,这样客户端浏览器才能正确显示下载的进度 byte[] buffer; long hasRead = 0;//变量hasRead用于记录已经发送了多少字节的数据到客户端浏览器 //如果hasRead小于contentLength,说明下载文件还没读取完毕,继续循环读取下载文件的内容,并发送到客户端浏览器 while (hasRead < contentLength) { //HttpContext.RequestAborted.IsCancellationRequested可用于检测客户端浏览器和ASP.NET Core服务器之间的连接状态,如果HttpContext.RequestAborted.IsCancellationRequested返回true,说明客户端浏览器中断了连接 if (httpContext.RequestAborted.IsCancellationRequested) { //如果客户端浏览器中断了到ASP.NET Core服务器的连接,这里应该立刻break,取消下载文件的读取和发送,避免服务器耗费资源 break; } buffer = new byte[bufferSize]; int currentRead = fs.Read(buffer, 0, bufferSize); //从下载文件中读取bufferSize(1024字节)大小的内容到服务器内存中 response.Body.Write(buffer, 0, currentRead); //发送读取的内容数据到客户端浏览器 response.Body.Flush(); //注意每次Write后,要及时调用Flush方法,及时释放服务器内存空间 hasRead += currentRead; //更新已经发送到客户端浏览器的字节数 } } } } else { res.Data = "该文件不存在"; } res.Code = 200; return(res); }
public async Task <HttpReponseViewModel <GenerateMD5ToBurstResViewModel> > GetMD5ToBurstData(string filePath) { filePath = @"D:\Files\BI_V2.0.zip"; //filePath = @"D:\Files\Files.zip"; HttpReponseViewModel <GenerateMD5ToBurstResViewModel> res = new HttpReponseViewModel <GenerateMD5ToBurstResViewModel>(); return(await Task.Run(() => { long len = FileUtil.GetLength(filePath); res.Code = 20000; res.Data = new GenerateMD5ToBurstResViewModel() { Identifier = FileUploadUtil.GetMD5HashFromFile(filePath), TotalSize = len, FileName = filePath, FileRanges = _downloadServices.GetFileRangeData(len) }; return res; })); }
public async Task <HttpReponseViewModel> DeleteStoreFiles(Guid id) { HttpReponseViewModel httpReponse = null; var ent = await _baseDomain.GetEntity(id); var urlPath = ent.RelationFilePath; //删除文件 FileUtil.DeleteFiles(urlPath); var flag = FileUtil.IsExistFile(urlPath); if (!flag) { var f = await _baseDomain.DeleteDomain(id); httpReponse = new HttpReponseViewModel(f); } else { httpReponse = new HttpReponseViewModel(false, "", "文件未删除"); } return(httpReponse); }
/// <summary> /// 文件合并 /// </summary> /// <param name="fileUpload"></param> /// <returns></returns> public async Task <HttpReponseViewModel <StoreFilesViewModel> > MergeFiles(FileUploadReqViewModel fileUpload) { HttpReponseViewModel <StoreFilesViewModel> res = new HttpReponseViewModel <StoreFilesViewModel>(); try { var identifier = fileUpload.Identifier; var fileName = fileUpload.FileName; var webRoot = AppConfigUtil.FilePath; //源数据文件夹 string sourcePath = FileUploadUtil.GetFileMd5Folder(webRoot, identifier); //合并后的文件路径 string targetFilePath = sourcePath + Path.GetExtension(fileName); // 目标文件不存在,则需要合并 if (!FileUtil.IsExistFile(targetFilePath)) { if (!FileUtil.IsExistDirectory(sourcePath)) { res.Message = "为找到文件"; res.ResultSign = ResultSign.Error; res.Code = 200; return(res); } FileUploadUtil.MergeDiskFile(sourcePath, targetFilePath); } fileUpload.RelativePath = targetFilePath; var valid = VaildMergeFile(fileUpload); FileUploadUtil.DeleteFolder(sourcePath); //返回文件新的路径 var newFilePath = FileUploadUtil.newFilePath(webRoot, targetFilePath, fileName); //存储成功 if (newFilePath != "") { StoreFiles storeFiles = fileUpload.MapTo <StoreFiles>(); storeFiles.ID = Guid.NewGuid(); storeFiles.RelationFilePath = newFilePath; storeFiles.CreateBy = ""; storeFiles.FileName = fileName.Replace("." + storeFiles.FileExt, ""); res.Flag = await _baseDomain.AddDomain(storeFiles); if (res.Flag) { res.ResultSign = ResultSign.Success; res.Data = storeFiles.MapTo <StoreFilesViewModel>(); } else { res.ResultSign = ResultSign.Error; } } res.Code = 200; return(res); } catch (Exception ex) { return(res); } }
public async Task <HttpReponseViewModel <FileUploadResViewModel> > ChunkUpload(IFormFile file) { HttpReponseViewModel <FileUploadResViewModel> res = new HttpReponseViewModel <FileUploadResViewModel>(); string identifier = ""; int chunkNumber = 0; int totalChunks = 0; string fileName = ""; int currentChunkSize = 0; int chunkSize = 0; int totalSize = 0; string relativePath = ""; try { foreach (var item in Request.Form.Keys) { if (item == "chunkNumber") { chunkNumber = Convert.ToInt32(Request.Form["chunkNumber"].ToString()); } if (item == "identifier") { identifier = Request.Form["identifier"].ToString(); } if (item == "totalChunks") { totalChunks = Convert.ToInt32(Request.Form["totalChunks"].ToString()); } if (item == "filename") { fileName = Request.Form["filename"].ToString(); } if (item == "chunkSize") { chunkSize = Convert.ToInt32(Request.Form["chunkSize"].ToString()); } if (item == "currentChunkSize") { currentChunkSize = Convert.ToInt32(Request.Form["currentChunkSize"].ToString()); } if (item == "totalSize") { totalSize = Convert.ToInt32(Request.Form["totalSize"].ToString()); } if (item == "relativePath") { relativePath = Request.Form["relativePath"].ToString(); } } FileUploadReqViewModel fileUpload = new FileUploadReqViewModel(); fileUpload.Identifier = identifier; fileUpload.FileName = fileName; fileUpload.ChunkNumber = chunkNumber; fileUpload.ChunkSize = chunkSize; fileUpload.CurrentChunkSize = currentChunkSize; fileUpload.RelativePath = relativePath; fileUpload.TotalChunks = totalChunks; fileUpload.TotalSize = totalSize; fileUpload.File = file; return(await Task.Run(() => { return _fileUploadServices.ChunkUpload(fileUpload); })); } catch (Exception ex) { res.Flag = false; res.Code = 500; res.Data = null; return(res); } }