我是靠谱客的博主 飞快向日葵,最近开发中收集的这篇文章主要介绍C# .NETCore、EF、API接口 后端实现简单分片式文件上传,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 参考代码如下:


/// <summary>
/// 模型资源上传
/// </summary>
/// <param name="uploadmodel"></param>
/// <returns></returns>
[Route("ThreeDUpload")]
[HttpPost]
public async Task<ActionResult> UploadImage([FromHeader] downloadmodel uploadmodel)
{
FileModel result = new FileModel();
var data = uploadmodel.file;
var total = uploadmodel.totalSlices;
var fileName = uploadmodel.filename;
var index = uploadmodel.index;
string fileExt = Path.GetExtension(fileName);//获取文件后缀
var resourelist = _context.Resourcestype.Where(x => x.Id == uploadmodel.resouretypeid).FirstOrDefault();//获取上级文件夹名字
var TheSuperior = resourelist.ResourcesTypeName;
string temporary = Path.Combine(Directory.GetCurrentDirectory(), TheSuperior);
try
{
var finalPaths = DateTime.Now.ToString("yyMMddHHmmss") + fileName;
var finalPath = Path.Combine($"{Directory.GetCurrentDirectory()}/wwwroot/" + TheSuperior + "/" + finalPaths);
//最终的文件名
if (!Directory.Exists(temporary))
Directory.CreateDirectory(temporary);
string filePath = Path.Combine(temporary, index.ToString());
if (!Convert.IsDBNull(data))
{
await Task.Run(() =>
{
FileStream fs = new FileStream(filePath, FileMode.Create);
data.CopyTo(fs);
fs.Flush();
fs.Close();
});
StaticM.index += 1;
}
bool mergeOk = false;
if (StaticM.index == uploadmodel.totalSlices)
{
mergeOk = await FileMerge(TheSuperior, fileName, finalPath);
StaticM.index = 0;
}
result.Url = "/" + TheSuperior;
var newFileName = result.Url;
return new ContentResult { ContentType = "multipart/form-data", Content = newFileName + "/" + finalPaths };
}
catch (Exception ex)
{
Directory.Delete(temporary);//删除文件夹
throw ex;
}
}
public async Task<bool> FileMerge(string TheSuperior, string fileName , string finalPath)
{
bool ok = false;
try
{
var temporary = Path.Combine(Directory.GetCurrentDirectory(), TheSuperior);//临时文件夹
var files = Directory.GetFiles(temporary);//获得下面的所有文件
var fs = new FileStream(finalPath, FileMode.Create);
foreach (var part in files.OrderBy(x => x.Length).ThenBy(x => x))
//排一下序,保证从0-N Write
{
var bytes = System.IO.File.ReadAllBytes(part);
await fs.WriteAsync(bytes, 0, bytes.Length);
bytes = null;
System.IO.File.Delete(part);//删除分块
}
fs.Flush();
fs.Close();
Directory.Delete(temporary);//删除文件夹
ok = true;
}
catch (Exception ex)
{
throw ex;
}
return ok;
}

 downloadmodel 实体类:


public class downloadmodel
{
/// <summary>
/// 文件
/// </summary>
public IFormFile file { get; set; }
/// <summary>
/// 绑定的Id
/// </summary>
public int resouretypeid { get; set; }
/// <summary>
/// 当前
/// </summary>
public int index { get; set; }
/// <summary>
/// 总数
/// </summary>
public int totalSlices { get; set; }
/// <summary>
/// 文件名
/// </summary>
public string filename { get; set; }
}

最后

以上就是飞快向日葵为你收集整理的C# .NETCore、EF、API接口 后端实现简单分片式文件上传的全部内容,希望文章能够帮你解决C# .NETCore、EF、API接口 后端实现简单分片式文件上传所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部