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

    [HttpPost("[action]")]
            public async Task<dynamic> NewUploadFile(IFormFile fileinput)
            {
                try
                {
                    if (fileinput == null)
                    {
                        return new { code = ApiCode.Perror, message = "文件为空!" };
                    }
                    // 原文件名(包括路径)
                    var filename = ContentDispositionHeaderValue.Parse(fileinput.ContentDisposition).FileName;
                    // 扩展名
                    var extName = filename.Substring(filename.LastIndexOf('.')).Replace(""", "");
                    // 新文件名
                    //string shortfilename = $"{Guid.NewGuid()}{extName}";
                    // 新文件名(包括路径)
                    filename = Environment.WebRootPath + @"upload" + filename;
                    //判断路径是否存在
                    if (!Directory.Exists(Environment.WebRootPath + @"upload"))
                        Directory.CreateDirectory(Environment.WebRootPath + @"upload");
                    // 创建新文件
                    using (FileStream fs = System.IO.File.Create(filename))
                    {
                        // 复制文件
                        fileinput.CopyTo(fs);
                        // 清空缓冲区数据
                        fs.Flush();
                        return new { code = ApiCode.Success, message = "文件上传成功!" };
                    }
    
                }
                catch (Exception e)
                {
                    Logger.LogInformation(e + "
    ");
                    return new { code = ApiCode.Exception, message = "服务器内部异常,请联系管理员!" };
                }
    
            }
    

      

  • 相关阅读:
    AtCoder agc023_f
    CodeForces 1328
    洛谷 P4437
    Spark读取txt文件跳过第一行
    斯特林数学习笔记。
    hackrank subsets
    题解 CF1004F 【Sonya and Bitwise OR】
    [NOI2020]美食家
    Educational Codeforces Round 94 题解
    Delphi 与 C/C++ 数据类型对照表
  • 原文地址:https://www.cnblogs.com/qudeqiang/p/9288599.html
Copyright © 2011-2022 走看看