Guid.NewGuid().ToString()(随机文件名)

Guid .NewGuid().ToString()可生成类似一串 "12f5cdbd-2a6d-4ba6-a394-6961e4f5d5b5" 的字符串。

==================================

可用于上传文件生成新的文件名

Eg:

       string newfilename = Guid .NewGuid().ToString() +Path .GetExtension(filename);

                     Path .GetExtension(filename)       指定路径字符串的扩展名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.IO; using System.Linq; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace FileUpload.Controllers { [ApiController] public class UploadController : ControllerBase { private const int ChunkSize = 1024 * 1024 * 1; // 每个分片的大小,这里设为1MB private const string UploadPath = "uploads"; // 文件上传目录 private static string _filePath; // 完整的文件路径 [HttpPost("/upload/start")] public ActionResult StartUpload(IFormFile file) { if (file == null || file.Length <= 0) { return BadRequest("请选择要上传的文件"); } // 生成文件名 string fileName = file.FileName; string fileExt = Path.GetExtension(fileName); string newFileName = Guid.NewGuid().ToString("N") + fileExt; // 生成文件存储目录 string dirPath = Path.Combine(Directory.GetCurrentDirectory(), UploadPath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } // 生成文件路径 _filePath = Path.Combine(dirPath, newFileName); // 返回上传开始的响应 return Ok(new { FileName = newFileName, ChunkSize, }); } [HttpPost("/upload/append")] public ActionResult AppendUpload(string fileName, int chunkIndex, int chunks, IFormFile chunk) { byte[] buffer = new byte[ChunkSize]; int bytesRead = 0; int start = chunkIndex * ChunkSize; // 分片开始位置 int end = Math.Min(start + ChunkSize, (int)chunk.Length); // 分片结束位置 using (Stream stream = chunk.OpenReadStream()) { using (FileStream fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { fileStream.Position = start; while ((bytesRead = stream.Read(buffer, 0, Math.Min(buffer.Length, end - start))) > 0) { fileStream.Write(buffer, 0, bytesRead); start += bytesRead; } fileStream.Flush(true); } } // 检查是否所有分片都上传成功 int[] uploadedChunks = Directory.GetFiles(Path.GetDirectoryName(_filePath), $"{Path.GetFileName(_filePath)}_*").Select(filepath => int.Parse(filepath.Split('_')[1])).ToArray(); if (uploadedChunks.Length == chunks) { // 合并分片 using (FileStream fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { foreach (int index in uploadedChunks.OrderBy(i => i)) { string chunkPath = $"{_filePath}_{index}"; using (FileStream chunkStream = new FileStream(chunkPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { chunkStream.CopyTo(fileStream); } System.IO.File.Delete(chunkPath); // 删除已合并的分片 } fileStream.Flush(true); } } return Ok(); } } }
最新发布
04-21

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值