How to Upload Multiple Photos in Asp.net Using C# Using Folder
In this article, I am going to demonstrate you lot, how to upload file in ASP.NET MVC C# (you will run across both, single or multiple files upload examples in ASP.Net MVC C# ), if you are looking for image upload you tin can take a expect at this question https://qawithexperts.com/questions/14/how-to-upload-prototype-in-c-mvc.
Permit's understand File Upload Basics in ASP.NET MVC C#
During the file upload procedure, only 2 parts of the MVC model collaborate with each other – a view and a controller. Let's examine the file upload process pace by stride:
- A user visits a web folio with an uploader (represented by View) and chooses files to be uploaded.
- When the upload is started, the uploader packs the files into a Post asking and sends this request to the server.
- ASP.NET caches all data in server retentivity or to disk depending on the uploaded file size.
- ASP.NET MVC defines the controller and appropriate action method that will handle the asking.
- The activity method handles the request (for instance, saves files on a hd, or updates a database, etc.) through the
Controller.Requestbelongings, which gets theHttpPostedFilesBaseobject for the current request. - ASP.NET MVC sends an respond to the customer through
Controller.Response.
You lot can configure file upload settings by specifying advisable attributes in the spider web.config (or machine.config if yous want to make server-wide changes). Let's see what attributes are used to limit the file upload:
-
maxRequestLength– the asking size limit in kilobytes (the default value is 4096 KB). -
requestLengthDiskThreshold– the limit of data buffered in the server retentivity in kilobytes (the default value is 80 KB). -
executionTimeout– the allowed execution time for the asking before being automatically shut down by ASP.NET (the default value is 110 seconds).
All these attributes should be specified in the <httpRuntime> department.
Single File Upload in ASP.Net MVC, Pace by Step Implementation
At present let's create a Project in Visual Studio to upload file
- Go to File->New->Project. Give a suitable name to the Awarding. Click OK.
- Select MVC Template from information technology
- Now Permit's add a folder to upload files on this Folder of the projection.
- Now let's Add a controller (
UploadFileController) and the Code in it to handle post request and handle File Upload
Click on 'Add' and then proper noun it (UploadFileController), add this code now
[HttpPost] public ActionResult Upload(HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) try { //Server.MapPath takes the absolte path of folder 'Uploads' string path = Path.Combine(Server.MapPath("~/Uploads"), Path.GetFileName(file.FileName)); //Save file using Path+fileName accept from above string file.SaveAs(path); ViewBag.Bulletin = "File uploaded successfully"; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } else { ViewBag.Message = "You have non specified a file."; } return View(); }Note: You may demand to Add
Organisation.IO referencein the above Controller - Add the Index View (Right click inside Index ActionMethod and then 'Add' View), for creating File-upload handler in HTML, and and so add the code below to in your view
<h2>UploadFile</h2> @using (Html.BeginForm ("Upload", "UploadFile", FormMethod.Mail, new { enctype = "multipart/course-data" })) { <label for="file">Upload File:</label> <input type="file" name="file" id="file"/><br><br> <input blazon="submit" value="Upload"/> <br><br> @ViewBag.Message }
Above image shows the output of the View.
Note: E'er remmeber to add "enctype = "multipart/form-data"" in your HTML form, if you forget to write this piece of lawmaking in your HTML, your files volition not be submitted on the C# controller. - In you browser view, browse a file using 'Cull File' button and try to upload a file past clicking 'Upload' button, here is the debugging screenshot of file uploading, which shows, file was uploaded successfully
That's y'all are done, check the 'Uploads' Folder in your project, you lot should constitute the file there
Notation: The in a higher place code doesn't implement whatever security, yous should scan your uploaded file using Viruses for improve security
Multiple File Upload in ASP.NET MVC C#
The above example shows to upload single file in asp.net MVC, so basically nosotros would accept to loop multiple file uploads in C# code, while in front make our HTML to multiple files, here are the changes which you demand to do in C#
[HttpPost] //Now we are getting assortment of files cheque sign [] public ActionResult UploadFiles(HttpPostedFileBase[] files) { //iterating through multiple file collection foreach (HttpPostedFileBase file in files) { //Checking file is available to save. if (file != null) { var InputFileName = Path.GetFileName(file.FileName); var ServerSavePath = Path.Combine(Server.MapPath("~/Uploads/") + InputFileName); //Salvage file to server folder file.SaveAs(ServerSavePath); //assigning file uploaded status to ViewBag for showing message to user. ViewBag.UploadStatus = files.Count().ToString() + " files uploaded successfully."; } } return View(); } In the above code, since nosotros are uploading files at once, we are passing HttpPostedFileBase array of files, and so iterating each file using foreach loop, and saving file on server folder ("Uploads").
In HTML/View udpated .cshtml code volition be like below
<br/> @using (Html.BeginForm("UploadFiles", "UploadFile", FormMethod.Postal service, new { enctype = "multipart/grade-information" })) { <div class="form-horizontal"> <div class="class-grouping"> <div class="col-lg-2">Browse Files</div> <div class="col-md-10"> <input type="file" multiple proper name="files" id="Files" /> </div> </div> <div form="form-grouping"> <div class="col-md-offset-2 col-md-x"> <input blazon="submit" value="Upload" grade="btn btn-chief" /> </div> </div> <div class="form-group"> <div course="col-dr.-offset-2 col-md-10 text-success"> @ViewBag.UploadStatus </div> </div> </div> } if you will notice, we have just added multiple an attribute in <input type='file'>above HTML code, to make information technology select multiple files, hither is the sample View prototype
After selecting all file and clicking on Upload, here is the paradigm of Controller while debugging, you can see we have got iii files in Controller this time
That'southward it, we are done.
You may also like to read
File uploading using DropZone js & HTML5 in MVC
File Uploading using jQuery Ajax in MVC (Single or multiple file upload)
File upload in ASP.NET Cadre MVC (Single or Multiple files)
That'southward it we are done, feel free to post your comments on the in a higher place commodity or ask whatsoever questions related to file uploading in ASP.Internet MVC.
lambersonpecausely.blogspot.com
Source: https://qawithexperts.com/article/asp.net/uploading-files-in-aspnet-mvc-c-single-multiple-files/67
0 Response to "How to Upload Multiple Photos in Asp.net Using C# Using Folder"
Postar um comentário