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

    }
    }
    }

  • 相关阅读:
    Eclipse consle 输出语句中,误输中文,假死问题
    Eclipse 快捷键 (最实用)
    mysql 4种启动方式
    mysql索引的类型和优缺点
    Windows上 使用Composer安装tp5
    php 更新配置文件
    可视化工具连接Linux上的redis
    HttpClient 4 教程 第3章 HTTP状态管理
    HttpClient 4 教程 第2章 连接管理
    HttpClient 4 教程 第1章 基础
  • 原文地址:https://www.cnblogs.com/tianxujun/p/12090124.html
Copyright © 2011-2022 走看看