MVC 6 HttpPostedFileBase?

2019-01-13 15:05发布

问题:

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 and did not have any luck. Does anyone know the correct way to upload a file in MVC6?

回答1:

MVC 6 used another mechanism to upload files. You can get more examples on GitHub or other sources. Just use IFormFile as a parameter of your action or a collection of files or IFormFileCollection if you want upload few files in the same time:

public async Task<IActionResult> UploadSingle(IFormFile file)
{
    FileDetails fileDetails;
    using (var reader = new StreamReader(file.OpenReadStream()))
    {
        var fileContent = reader.ReadToEnd();
        var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
        var fileName = parsedContentDisposition.FileName;
    }
    ...
}

[HttpPost]
public async Task<IActionResult> UploadMultiple(ICollection<IFormFile> files)
{
    var uploads = Path.Combine(_environment.WebRootPath,"uploads"); 
    foreach(var file in files)
    {
        if(file.Length > 0)
        {
            var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            await file.SaveAsAsync(Path.Combine(uploads,fileName));
        }
        ...
    }
}

You can see current contract of IFormFile in asp.net sources. See also ContentDispositionHeaderValue for additional file info.



回答2:

There is no HttpPostedFileBase in MVC6. You can use IFormFile instead.

Example: https://github.com/aspnet/Mvc/blob/dev/test/WebSites/ModelBindingWebSite/Controllers/FileUploadController.cs

Snippet from the above link:

public FileDetails UploadSingle(IFormFile file)
{
    FileDetails fileDetails;
    using (var reader = new StreamReader(file.OpenReadStream()))
    {
        var fileContent = reader.ReadToEnd();
        var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
        fileDetails = new FileDetails
        {
            Filename = parsedContentDisposition.FileName,
            Content = fileContent
        };
    }

    return fileDetails;
}


回答3:

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<IActionResult> 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;
    }