/// <summary> /// 添加文件 /// </summary> /// <param name="fileCollectionDto"></param> /// <returns></returns> public async Task <string> AddFileRecord(FileCollectionDto fileCollectionDto) { var entity = _mapper.Map <FileCollectionDto, FileCollectionEntity>(fileCollectionDto); entity.Init(); entity.CreateUserId = "SYSTEM"; _context.FileCollections.Add(entity); await _context.SaveChangesAsync(); return(entity.Id); }
public async Task <NormalResult <IList <string> > > UpLoadFile([FromForm] IFormCollection upfiles) { var getFiles = upfiles; var cpuId = Request.Form["CpuId"].ToString() ?? string.Empty; var userFilePath = Request.Form["UserFilePath"].ToString() ?? string.Empty; var files = Request.Form.Files.ToList(); long size = files.Sum(f => f.Length); // full path to file in temp location //var filePath = Path.GetTempFileName(); //var filePath = @"D:\UploadingFiles\" + formFile.FileName; var list = new List <string>(); list.AddRange(files.FindAll(c => c.Length > 0).ConvertAll(c => c.FileName)); #region 文件流 //foreach (var formFile in files) //{ // if (formFile.Length <= 0) continue; // using (var stream = new FileStream(filePath, FileMode.Create)) // { // await formFile.CopyToAsync(stream); // BinaryReader br = new BinaryReader(stream); // var byData = br.ReadBytes((int)stream.Length); // } //} #endregion #region 内存流 //foreach (var formFile in files) //{ // if (formFile.Length <= 0) continue; // //using (var stream = new MemoryStream()) // //{ // // await formFile.CopyToAsync(stream); // // BinaryReader br = new BinaryReader(stream); // // var byData = br.ReadBytes((int)stream.Length); // //} // using var stream = formFile.OpenReadStream(); // BinaryReader br = new BinaryReader(stream); // var byData = br.ReadBytes((int)stream.Length); //} #endregion #region 流 try { foreach (var formFile in files) { if (formFile.Length <= 0) { continue; } using var stream = formFile.OpenReadStream(); var savePath = Path.Combine("FileUpLoads", cpuId); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string saveFile = Guid.NewGuid().ToString("N"); BinaryReader br = new BinaryReader(stream); var byData = br.ReadBytes((int)stream.Length); //将下载下来的文件放在当前目录下,保存为e.wav;当然如果是图片,可以保存为a.jpg using FileStream fs = new FileStream(Path.Combine(savePath, saveFile + ".png"), FileMode.OpenOrCreate); MemoryStream m = new MemoryStream(byData); m.WriteTo(fs); var dto = new FileCollectionDto { CpuId = cpuId, FileName = formFile.FileName, FilePath = Path.Combine(savePath, saveFile + ".png"), UserFilePath = userFilePath, ImageFileStream = null }; await _fileCollectionService.AddFileRecord(dto); } } catch (Exception ex) { //吃掉 } #endregion // process uploaded files // Don't rely on or trust the FileName property without validation. return(new NormalResult <IList <string> > { Data = list }); }