189 8069 5689

C#.NETCore、EF、API接口后端实现简单分片式文件上传-创新互联

参考代码如下:

站在用户的角度思考问题,与客户深入沟通,找到瑶海网站设计与瑶海网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、做网站、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖瑶海地区。
////// 模型资源上传
        /////////[Route("ThreeDUpload")]
        [HttpPost]
        public async Task 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 TaskFileMerge(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
        {
            ////// 文件
            ///public IFormFile file { get; set; }
            ////// 绑定的Id
            ///public int resouretypeid { get; set; }
            ////// 当前
            ///public int index { get; set; } 
            ////// 总数
            ///public int totalSlices { get; set; }
            ////// 文件名
            ///public string filename { get; set; }

        }

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻标题:C#.NETCore、EF、API接口后端实现简单分片式文件上传-创新互联
文章位置:http://jkwzsj.com/article/dppsgi.html

其他资讯