zoukankan      html  css  js  c++  java
  • .net core api 文件上传

    public class FilesController : LedaoControllerBase
    {
    private Imagepath imagepath;
    public FilesController(Imagepath imagepath)
    {
    this.imagepath = imagepath;
    }
    #region MyRegion

    // GET: api/Files/5
    [HttpPost("UplodeFileOne1"), AllowAnonymous]
    public async Task<AjaxResponse> UplodeFile([FromForm]IFormFile fromFile)
    {
    var result = ToolHelp.UplodeFile(fromFile);
    return new AjaxResponse();
    }
    #endregion

    // 本地上传
    [HttpPost("UplodeFileOne"), AllowAnonymous]
    public async Task<AjaxResponse> UplodeFile01([FromForm] IFormFileCollection files)
    {
    try
    {
    long size = files.Sum(f => f.Length);
    var temp = "Upload/" + DateTime.Now.ToString("yyyy/MMdd");
    var fileFolder = Path.Combine(imagepath.Url, temp);
    if (!Directory.Exists(fileFolder))
    Directory.CreateDirectory(fileFolder);
    List<string> list = new List<string>();
    foreach (var file in files)
    {
    if (file.Length > 0)
    {
    var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") +
    Path.GetExtension(file.FileName);
    var filePath = Path.Combine(fileFolder, fileName);
    using (var stream = new FileStream(filePath, FileMode.Create))
    {
    await file.CopyToAsync(stream);
    }
    list.Add(temp + "/" + fileName);
    }
    }
    var result = new { Code = 2000, Msg = "成功", Data = list };
    return new AjaxResponse(result);
    }
    catch (Exception e)
    {
    return new AjaxResponse(new { Code = 2000, Msg = $"失败:{e.Message}", Data = new { } });

    }
    }
    }

  • 相关阅读:
    C# 反射
    jquery 循环绑定click的问题
    socket 编程
    EF code first出现错误:列名 Discriminator 无效
    C# 两个类是否继承关系
    C# MD5,hmacSHA1
    文件分块上传
    读写CSV到DataTable
    ajax 提交数组 泛型集合(嵌套集合)
    Json中Date映射到model
  • 原文地址:https://www.cnblogs.com/tianxujun/p/12090124.html
Copyright © 2011-2022 走看看