• 當前位置:首頁 > IT技術 > Web編程 > 正文

    .net core 文件上傳+導入
    2022-05-31 17:18:13

    文件上傳和導入

    控制器代碼

     [Route("api/[controller]")]
    ? [ApiController]
    ? public class UpdController : ControllerBase
    ? {
    ? ? ? private readonly IWebHostEnvironment env;
    ? ? ? private readonly IUserRespository userRespository;
    ? ? ? public UpdController(IWebHostEnvironment env, IUserRespository userRespository)
    ? ? ? {
    ? ? ? ? ? this.env = env;
    ? ? ? ? ? this.userRespository = userRespository;
    ? ? ? }
    ?
    ? ? ? [HttpPost]
    ? ? ? public IActionResult Upload(IFormFile formFile)
    ? ? ? {
    ? ? ? ? ? //先上傳 才能將內容導入數據庫
    ? ? ? ? ? //var file = request.Form.Files[0]
    ? ? ? ? ? //判斷擴展名 要求只能上傳 excel 獲取文件擴展名 引用system.IO
    ? ? ? ? ? //判斷文件擴展名為.xls(2003以前版本).xlsx(2007以后版本含2007)
    ? ? ? ? ? var ext = Path.GetExtension(formFile.FileName);
    ? ? ? ? ? if (ext == ".xls" || ext == ".xlsx")
    ? ? ? ? ? {
    ? ? ? ? ? ? ? try
    ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? //判斷文件大小
    ? ? ? ? ? ? ? ? ? //存儲路徑
    ? ? ? ? ? ? ? ? ? var path = Path.Combine(env.ContentRootPath, "Images");
    ? ? ? ? ? ? ? ? ? //判斷文件夾是否存在
    ? ? ? ? ? ? ? ? ? if (!Directory.Exists(path))
    ? ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? ? //創建文件夾
    ? ? ? ? ? ? ? ? ? ? ? Directory.CreateDirectory(path);
    ? ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? ? ? ? //文件的名稱
    ? ? ? ? ? ? ? ? ? var newName = DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
    ? ? ? ? ? ? ? ? ? //把文件名和路徑合并
    ? ? ? ? ? ? ? ? ? var pathName = Path.Combine(path, newName);
    ? ? ? ? ? ? ? ? ? //通過流的方式傳遞文件內容
    ? ? ? ? ? ? ? ? ? using (var stream = new FileStream(pathName, FileMode.Create))
    ? ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? ? formFile.CopyTo(stream);
    ? ? ? ? ? ? ? ? ? }
    ?
    ? ? ? ? ? ? ? ? ? //////導入
    ? ? ? ? ? ? ? ? ? ImportData(pathName);
    ? ? ? ? ? ? ? ? ? //返回當前的IP地址和端口號路徑和文件名
    ? ? ? ? ? ? ? ? ? return Ok(new { oleName = formFile.FileName, url = "http://localhost:56405/StaticFiles/" + newName, size = formFile.Length });
    ? ? ? ? ? ? ? }
    ? ? ? ? ? ? ? catch (Exception)
    ? ? ? ? ? ? ? {
    ?
    ? ? ? ? ? ? ? ? ? throw;
    ? ? ? ? ? ? ? }
    ? ? ? ? ? }
    ? ? ? ? ? else
    ? ? ? ? ? {
    ? ? ? ? ? ? ? return BadRequest("無效文件");
    ? ? ? ? ? }
    ? ? ? }
    ?
    ? ? ? private bool ImportData(string path)
    ? ? ? {
    ? ? ? ? ? try
    ? ? ? ? ? {
    ? ? ? ? ? ? ? //找到文件打開
    ? ? ? ? ? ? ? using (var stream = System.IO.File.OpenRead(path))
    ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? //獲取的是一個集合
    ? ? ? ? ? ? ? ? ? var rows = stream.Query<User>();
    ? ? ? ? ? ? ? ? ? //調用批量添加的方法
    ? ? ? ? ? ? ? ? ? return userRespository.AddBatchUser(rows);
    ? ? ? ? ? ? ? }
    ? ? ? ? ? }
    ? ? ? ? ? catch (Exception)
    ? ? ? ? ? {
    ?
    ? ? ? ? ? ? ? throw;
    ? ? ? ? ? }
    ? ? ? }
    ? }

    NuGet包

    Install-Package MiniExcel -Version 1.26.2

    參考網站

    https://gitee.com/dotnetchina/MiniExcel
    ?

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码