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 = "服务器内部异常,请联系管理员!" };
                }
    
            }
    

      

  • 相关阅读:
    实验七---类的多态
    实验六
    实验五---排序、质数
    实验四---杨辉三角
    node中间件KOA函数
    java文件名判断练习
    npm install 安装报错错误问题
    bundle is not defined 手动搭建项目架构(一)
    ztree实现拖拽功能
    js单线程 详解 来自知乎
  • 原文地址:https://www.cnblogs.com/qudeqiang/p/9288599.html
Copyright © 2011-2022 走看看