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

      

  • 相关阅读:
    php 魔术方法
    有用的函数系统采集(一)
    php后期静态绑定
    php对象引用及序列化
    php文件操作
    复习mysql视图总结
    jQuery调用WebService详解
    这个网站的界面描述甚好,我喜欢
    喜欢和技术有关的一切~
    Introduction
  • 原文地址:https://www.cnblogs.com/qudeqiang/p/9288599.html
Copyright © 2011-2022 走看看