MVC 6 HttpPostedFileBase?

后端 未结 3 1497
不思量自难忘°
不思量自难忘° 2020-12-04 23:36

I am attempting to upload an image using MVC 6; however, I am not able to find the class HttpPostedFileBase. I have checked the GitHub

3条回答
  •  有刺的猬
    2020-12-05 00:23

    I was searching around for quite a while trying to piece this together in .net core and ended up with the below. The Base64 conversion will be next to be done so that the retrieval and display is a little easier. I have used IFormFileCollection to be able to do multiple files.

    [HttpPost]
        public async Task Create(IFormFileCollection files)
        {
    
            Models.File fileIn = new Models.File();
            if(model != null && files != null)
            {
                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                      var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
    
                            byte[] fileBytes = null;
                            using (var fileStream = file.OpenReadStream())
                            using (var ms = new MemoryStream())
                            {
                                fileStream.CopyTo(ms);
                                fileBytes = ms.ToArray();
                                //string s = Convert.ToBase64String(fileBytes);
                                // act on the Base64 data
                            }
    
    
                            fileIn.Filename = fileName;
                            fileIn.FileLength = Convert.ToInt32(file.Length);
                            fileIn.FileType = file.ContentType;
                            fileIn.FileTypeId = model.FileTypeId;
                            fileIn.FileData = fileBytes;
                            _context.Add(fileIn);
                            await _context.SaveChangesAsync();
                    }
                }
            }
            return View();
        }
    

    EDIT

    And below is return of files to a list and then download.

    public JsonResult GetAllFiles()
        {
    
            var files = _context.File
                .Include(a => a.FileCategory)
                .Select(a => new
                {
                    id = a.Id.ToString(),
                    fileName = a.Filename,
                    fileData = a.FileData,
                    fileType = a.FileType,
                    friendlyName = a.FriendlyName,
                    fileCategory = a.FileCategory.Name.ToLower()
                }).ToList();
    
           return Json(files);
        }
    
        public FileStreamResult DownloadFileById(int id)
        {
            // Fetching file encoded code from database.
            var file = _context.File.SingleOrDefault(f => f.Id == id);
            var fileData = file.FileData;
            var fileName = file.Filename;
    
            // Converting to code to byte array
            byte[] bytes = Convert.FromBase64String(fileData);
    
            // Converting byte array to memory stream.
            MemoryStream stream = new MemoryStream(bytes);
    
            // Create final file stream result.
            FileStreamResult fileStream = new FileStreamResult(stream, "*/*");
    
            // File name with file extension.
            fileStream.FileDownloadName = fileName;
            return fileStream;
        }
    

提交回复
热议问题