Upload Multiple files using Asp.net mvc

first create a folder with the name of 'product images' in root of the project

aspmvcsnippets.com file upload tutorial

controller code

create a new controller in controller folder and first controller to access file upload view.

public actionresult uploadfiles()
        {
           return view();
        }

view code

now right click on view() and add view. accept default settings and click 'add'. now add razor code for uploading files.

@using (html.beginform("savefiles", "product", formmethod.post, new { enctype = "multipart/form-data", @class = "contact-form" }))
 {
    @html.antiforgerytoken()
    
  <input name="uploadfiles" required id="productimages" accept="images/*" type="file" multiple width="200" />
  <input type="submit" value="upload files"/>
}

controller code

now write controller code to accept uploaded file and move to destination folder

 [httppost]
        [validateantiforgerytoken]
        public actionresult savefiles()
        {
            for (int i = 0; i < request.files.count; i++)
            {
                
                httppostedfilebase file = request.files[i];
                string path = system.io.path.combine(server.mappath("~/product images"), file.filename);
                file.saveas(path);
               
            }
            return view();
        }

httppostedfilebase class used to upload files from client to server. here we loop over request array and fetch all the files and upload on folder one by one.

view code

now click on savefiles() view and add a new success view

<h2>files uploaded successfully.</h2>