zoukankan      html  css  js  c++  java
  • webapi上次文件

    //控制器

    [HttpPost]
    [Route("image/uploads")]
    public async Task<string[]> Uploads()
    {
    // 检查是否是 multipart/form-data
    if (!Request.Content.IsMimeMultipartContent("form-data"))
    {
    throw new FileUploadException("请求的数据格式不正确");
    }
    var paths = await _uploader.Upload(Request.Content);
    if (!paths.Any())
    {
    throw new FileUploadException("上传失败");
    }
    return paths;
    }
    [HttpPost]
    [Route("image/uploadOss")]
    public async Task<string> UploadOss()
    {
    var httpRequest = HttpContext.Current.Request;
    // 检查是否是 multipart/form-data
    if (!Request.Content.IsMimeMultipartContent("form-data"))
    {
    throw new FileUploadException("请求的数据格式不正确");
    }
    var paths = await _uploader.PutObjectFromFile(httpRequest);

    if (!paths.Any())
    {
    throw new FileUploadException("上传失败");
    }
    return paths.First();
    }

    //api 接口

    public async Task<string[]> Upload(HttpContent content)
    {
    //文件存储地址
    string dirPath = BaseDirectory + RelativePath;
    if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);

    // 采用MultipartMemoryStreamProvider
    var provider = new CustomMultipartFormDataStreamProvider(dirPath);
    try
    {
    // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
    await content.ReadAsMultipartAsync(provider);
    var savePaths = provider.FileData.Select(p => RelativePath + Path.GetFileName(p.LocalFileName)).ToArray();
    //对已保存的文件进行处理
    var resultPaths = FileHandler(savePaths).Select(p => ServerHost + p).ToArray();

    return resultPaths;
    }
    catch { return new string[] { }; }
    }

    /// <summary>
    /// 上传文件到阿里云oss
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public async Task<string[]> PutObjectFromFile(HttpRequest content)
    {
    try
    {
    var pahts = new List<string>();
    if (content.Files.Count <= 0) return pahts.ToArray();
    foreach (string file in content.Files)
    {
    var postedFile = content.Files[file];
    var path = RelativePath + DateTime.Now.ToString("yyyyMMddHHmmssfff")+"."+ postedFile.FileName.Split('.')[1];
    client.PutObject(BucketName, path, postedFile.InputStream);
    pahts.Add(Path.Combine("http://cczc-static.oss-cn-beijing.aliyuncs.com/", path));
    }
    return pahts.ToArray();
    }
    catch { return new string[] { }; }
    }

    //参数配置

    protected static readonly string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
    protected static readonly string ServerHost = ConfigurationManager.AppSettings["ServerHost"];
    protected static readonly string BucketName = ConfigurationManager.AppSettings["BucketName"];

    /// <summary>
    /// 文件上传相对路径,子类可重写
    /// </summary>
    /// <returns></returns>
    protected virtual string RelativePath { get; } = $"upload/image/{DateTime.Today:yyyyMM/dd}/";

    protected static string Endpoint { get; } = “你的阿里云访问域名”

    protected static string AccessKeyId { get; } = AccessKeyId ;

    protected static string AccessKeySecret { get; } = AccessKeySecret;

    protected static OssClient client = new OssClient(Endpoint, AccessKeyId, AccessKeySecret);

  • 相关阅读:
    查windows系统开关机记录
    HDU-6278-Jsut$h$-index(主席树)
    POJ-2104-Kth Number(主席树)
    HDU-6546-Function(贪心)
    POJ-1523-SPF(求割点)
    POJ-2762-Going from u to v or from v to u(强连通, 拓扑排序)
    POJ-2552-The Bottom of a Graph 强连通分量
    POJ-1659-Frogs' Neighborhood
    POJ-1904-King‘s Quest
    POJ-1236-Network of Schools
  • 原文地址:https://www.cnblogs.com/xlin/p/8269128.html
Copyright © 2011-2022 走看看