c# .net core 简易上传文件api

临时简单写了个   可以根据自己需求做修改!
        public string PostUpload(List<IFormFile> files)
        {
            var file = files[0];
            var ext = Path.GetExtension(file.FileName).ToLower();
            if (ext != ".txt")
            {
                throw Oops.Oh($"上传文件发生错误! 只能上传txt文本文件!");
            }

            var path = "upload/" + DateTime.Now.ToString("yyyyMM");

            Directory.CreateDirectory(path);

            var pathname = path + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
            using (var stream = new FileStream(pathname, FileMode.Create))
            {
                file.CopyTo(stream);
            }

            return pathname;
        }
posted @ 2021-01-22 23:45  ToLing·  阅读(541)  评论(0编辑  收藏  举报