Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process of choosing and transferring numerous images from a webpage to outside applications or a nearby directory for direct download through ASP.NET Core MVC involves the following steps:

  1. Create a form on the webpage that allows users to select multiple images they want to download.
  2. Use the input type "file" to allow users to browse and select images from their local storage.
  3. Once a user selects the images, use JavaScript to retrieve the file data and submit it to the server using AJAX.
  4. On the server-side, use ASP.NET Core MVC to receive the uploaded files and save them to a specified directory.
  5. Use a file download helper method in ASP.NET Core MVC to allow users to directly download the saved images.

Here's a sample code in ASP.NET Core MVC that shows how to receive and save uploaded files:

[HttpPost]
public async Task<IActionResult> UploadFiles(List<IFormFile> files)
{
    long size = files.Sum(f => f.Length);

    foreach (var formFile in files)
    {
        if (formFile.Length > 0)
        {
            // Save the file to a specified directory
            var filePath = Path.Combine("C:/Images/", formFile.FileName);
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await formFile.CopyToAsync(stream);
            }
        }
    }

    // Return a response indicating successful upload
    return Ok(new { count = files.Count, size });
}