Asp.Net Core WebApi 和Asp.Net WebApi上传文件

 1    public class UpLoadController : ControllerBase
 2     {
 3         private readonly IHostingEnvironment _hostingEnvironment;
 4 
 5         public UpLoadController(IHostingEnvironment hostingEnvironment)
 6         {
 7             _hostingEnvironment = hostingEnvironment;
 8         }        
 9         [HttpPost]
10         public async Task<string> Post([FromForm] IFormCollection formCollection)
11         {
12             string result = "";
13             string webRootPath = _hostingEnvironment.WebRootPath;
14             string contentRootPath = _hostingEnvironment.ContentRootPath;
15 
16             FormFileCollection filelist = (FormFileCollection)formCollection.Files;
17 
18             foreach (IFormFile file in filelist)
19             {
20                 String Tpath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
21                 string name = file.FileName;
22                 string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
23                 string FilePath = webRootPath + Tpath;
24 
25                 string type = System.IO.Path.GetExtension(name);
26                 DirectoryInfo di = new DirectoryInfo(FilePath);
27 
28 
29                 if (!di.Exists) { di.Create(); }
30 
31                 using (FileStream fs = System.IO.File.Create(FilePath + FileName + type))
32                 {
33                     // 复制文件
34                     file.CopyTo(fs);
35                     // 清空缓冲区数据
36                     fs.Flush();
37                 }
38                 result = "1";
39             }
40             return result;
41         }
42     }
.NetCore WebApi
[HttpPost]
        public async Task<string> Post()
        {
            string result = "";
            HttpFileCollection filelist = HttpContext.Current.Request.Files;
            if (filelist != null && filelist.Count > 0)
            {
                for (int i = 0; i < filelist.Count; i++)
                {
                    HttpPostedFile file = filelist[i];
                    String Tpath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                    string filename = file.FileName;
                    string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    string FilePath = HttpContext.Current.Server.MapPath("~/" + Tpath);
                    string type = System.IO.Path.GetExtension(filename);
                    DirectoryInfo di = new DirectoryInfo(FilePath);
                    if (!di.Exists) { di.Create(); }
                    try
                    {
                        file.SaveAs(FilePath + FileName+type);
                        result = "1";
                    }
                    catch (Exception ex)
                    {
                        result = "上传文件写入失败:" + ex.Message;
                    }
                }
            }
            else
            {
                result = "上传的文件信息不存在!";
            }
            return result;
        }
Asp.Net WebApi

 前端插件:https://blog.csdn.net/u010075697/article/details/72771594

转载于:https://www.cnblogs.com/cyh92/p/9798155.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值