コード例 #1
0
        public async Task <IActionResult> UploadFile(List <IFormFile> files)
        {
            string virtualPath = "";

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string guid  = Guid.NewGuid().ToString("n");
                    string exten = Path.GetExtension(formFile.FileName);
                    virtualPath = string.Format("/fileupload/{0}/", guid);
                    string mapPth  = _host.WebRootPath + virtualPath;
                    string filePth = _host.WebRootPath + virtualPath + formFile.FileName;
                    Directory.CreateDirectory(Path.GetDirectoryName(mapPth));

                    using (var stream = new FileStream(filePth, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }

                    //添加数据
                    FileMgmt entity = new FileMgmt();
                    entity.Name       = formFile.Name;
                    entity.Length     = (int)formFile.Length;
                    entity.Url        = virtualPath;
                    entity.Guid       = guid;
                    entity.CreateTime = DateTime.Now;
                    try
                    {
                        _context.FileMgmt.Add(entity);
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            return(Json(new { path = virtualPath }));
        }
コード例 #2
0
 public async Task <int> SaveChangesAsync() => await _context.SaveChangesAsync();