1.开发分片上传接口

master
xiaochanghai 1 year ago
parent 26d8dc9e37
commit bf4fe5d7ad
  1. 18
      Tiobon.Core.Api/Controllers/FileController.cs
  2. 5
      Tiobon.Core.IServices/Ghre/IGhre_AttachmentServices.cs
  3. 78
      Tiobon.Core.Services/Ghre/Ghre_AttachmentServices.cs

@ -145,4 +145,22 @@ public class FileController : BaseApiController
}
#endregion
#region 分片上传
/// <summary>
/// 分片上传
/// </summary>
/// <param name="file"></param>
/// <param name="videoName"></param>
/// <param name="chunkIndex"></param>
/// <param name="totalChunks"></param>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("UploadLarge")]
public async Task<IActionResult> UploadLargeAsync(IFormFile file, string videoName, int chunkIndex, int totalChunks, string id)
{
dynamic obj = await _ghre_AttachmentServices.UploadVideoAsync(file, videoName, chunkIndex, totalChunks, id);
return Ok(obj);
}
#endregion
}

@ -1,4 +1,5 @@
using Tiobon.Core.IServices.BASE;
using Microsoft.AspNetCore.Http;
using Tiobon.Core.IServices.BASE;
using Tiobon.Core.Model.Models;
namespace Tiobon.Core.IServices
@ -8,5 +9,7 @@ namespace Tiobon.Core.IServices
/// </summary>
public interface IGhre_AttachmentServices :IBaseServices<Ghre_Attachment, Ghre_AttachmentDto, InsertGhre_AttachmentInput, EditGhre_AttachmentInput>
{
Task<dynamic> UploadVideoAsync(IFormFile file, string videoName, int chunkIndex, int totalChunks, string id);
}
}

@ -3,12 +3,15 @@ using Tiobon.Core.IServices;
using Tiobon.Core.Model.Models;
using Tiobon.Core.Services.BASE;
using Tiobon.Core.IRepository.Base;
using Microsoft.AspNetCore.Http;
using System.Dynamic;
using SqlSugar;
namespace Tiobon.Core.Services
{
/// <summary>
/// 附件 (服务)
/// </summary>
/// <summary>
/// 附件 (服务)
/// </summary>
public class Ghre_AttachmentServices : BaseServices<Ghre_Attachment, Ghre_AttachmentDto, InsertGhre_AttachmentInput, EditGhre_AttachmentInput>, IGhre_AttachmentServices
{
private readonly IBaseRepository<Ghre_Attachment> _dal;
@ -17,5 +20,74 @@ namespace Tiobon.Core.Services
this._dal = dal;
base.BaseDal = dal;
}
public async Task<dynamic> UploadVideoAsync(IFormFile file, string videoName, int chunkIndex, int totalChunks, string id)
{
dynamic obj = new ExpandoObject();
string status = "error";
string message = string.Empty;
string pathHeader = string.Empty;
string url = string.Empty;
try
{
var path = $"{$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}upload{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}"}";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (var stream = File.Create(path + $"{chunkIndex}"))
{
await file.CopyToAsync(stream);
}
if (chunkIndex == totalChunks - 1)
await FileMerge(id, ".mp4", SnowFlakeSingle.Instance.NextId());
status = "ok";
}
catch (Exception E)
{
message = E.Message;
}
obj.url = url;
obj.status = status;
obj.message = message;
return obj;
}
public static async Task<object> FileMerge(string lastModified, string fileExts, long NewfileName)
{
string erro = "";
bool ok = false;
try
{
string wwwroot = $"{Directory.GetCurrentDirectory()}/wwwroot/";
var temporary = Path.Combine(wwwroot, lastModified);//临时文件夹
//fileName = Request.Form["fileName"];//文件名
string fileExt = fileExts;//获取文件后缀
var files = Directory.GetFiles(temporary);//获得下面的所有文件
DirectoryInfo di = new DirectoryInfo(wwwroot + NewfileName + "/");
if (!di.Exists)
di.Create();
var finalPath = Path.Combine(wwwroot + lastModified + "/", NewfileName + fileExt);//最终的文件名(demo中保存的是它上传时候的文件名,实际操作肯定不能这样)
var fs = new FileStream(finalPath, FileMode.Create);
foreach (var part in files.OrderBy(x => x.Length).ThenBy(x => x))//排一下序,保证从0-N Write
{
var bytes = File.ReadAllBytes(part);
await fs.WriteAsync(bytes, 0, bytes.Length);
bytes = null;
//System.IO.File.Delete(part);//删除分块
}
fs.Close();
//Directory.Delete(temporary);//删除文件夹
ok = true;
}
catch (Exception ex)
{
erro = ex.Message;
}
return ok;
}
}
}
Loading…
Cancel
Save