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 { } });

    }
    }
    }

  • 相关阅读:
    获取字符串出现的次数
    手机号码正则表达式验证
    DOM: EVENT FLOW
    AsyncCallback 异步回调委托
    高德地图
    我的json
    Arrow function restore
    constructor&object 的联系与对比
    for each/in/of的解释and example
    program发展史及以后预测
  • 原文地址:https://www.cnblogs.com/tianxujun/p/12090124.html
Copyright © 2011-2022 走看看