Exemple #1
0
        public async Task <IActionResult> StreamUploadAsync()
        {
            Console.WriteLine("StreamUpload上传了!");

            //获取Form提交的文件
            var  files = Request.Form.Files;
            long size  = files.Sum(f => f.Length);


            string webRootPath  = _hostEnvironment.WebRootPath; //物理路径
            string showFilePath = "";

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    long fileSize = formFile.Length;
                    //获得文件大小,以字节为单位
                    var file = new FileUpload
                    {
                        Id       = Guid.NewGuid(),
                        FileName = formFile.FileName
                    };

                    var filePath = webRootPath + "/upload";
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    filePath      = webRootPath + "/upload/" + file.FileName;
                    file.FilePath = filePath;
                    showFilePath  = "upload/" + file.FileName;

                    _dbContext.FileUploads.Add(file);
                    await _dbContext.SaveChangesAsync();

                    Directory.CreateDirectory(filePath);
                    //把上传的图片复制到指定的文件中
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                }
            }
            return(Ok(new
            {
                count = files.Count,
                savepath = showFilePath
            }));
        }
Exemple #2
0
        public async Task <bool> SaveAsync()
        {
            return((await _context.SaveChangesAsync()) >= 0);

            throw new NotImplementedException();
        }