.netcore之文件上传

.netcore 取消了之前.netframework的HttpPostedFileBase 。          

整理了一个上传文件的流程,可以选择跳转或不跳转页面。

  #引入jQuery以及 jQuery的jQuery.form.js,一定要先引入jQuery

       


                               

                                   

                                       

                                            选择恢复文件

                                                                                       
                                               
                                           

                                       


                                        
                                       
                                   

                               

                           

 

 

 

 

后台controller方法

 

 public async Task FileSave(List files)
        {
           // fil = files;
            var file = Request.Form.Files;
            long size = files.Sum(f => f.Length);
            string webRootPath = hostingEnvironment.WebRootPath;
            string contentRootPath = hostingEnvironment.ContentRootPath;
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string fileExt = "doc";
                    /// string fileExt = GetFileExt(formFile.FileName); //文件扩展名,不含“.”
                    long fileSize = formFile.Length; //获得文件大小,以字节为单位
                    string newFileName = System.Guid.NewGuid().ToString() + "." + fileExt; //随机生成新的文件名
                    var filePath = webRootPath + "/upload/" + formFile.FileName;
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {

                        await formFile.CopyToAsync(stream);
                    }
                }
            }
           
            return Json(filePath );
        }

 

你可能感兴趣的:(web程序,文件)