How to upload a Image in Asp.net MVC using Ajax request
-
How to upload a Image in Asp.net MVC using Ajax request I have single controller and its view file have to use Ajax request. Index Controller public ActionResult Index() { return View(); } and its view
-
Answer:
Html code for the input type file so we will call the input type from it id <input type="file" id="imageUploadForm" name="image" multiple="multiple" /> Ajax function $(document).ready(function () { $("#imageUploadForm").change(function () { var formData = new FormData(); var totalFiles = document.getElementById("imageUploadForm").files.length; for (var i = 0; i < totalFiles; i++) { var file = document.getElementById("imageUploadForm").files[i]; formData.append("imageUploadForm", file); } $.ajax({ type: "POST", url: '/Home/Upload', data: formData, dataType: 'json', contentType: false, processData: false, success: function (response) { alert('succes!!'); }, error: function (error) { alert("errror"); } }); }); }); Controller Function [HttpPost] public void Upload() { for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/App_Data/"), fileName); file.SaveAs(path); } }
user1157944 at Stack Overflow Visit the source
Related Q & A:
- How to Undo previous action in asp.net?Best solution by forums.asp.net
- How to upload the image to a server in Android PhoneGap?Best solution by Stack Overflow
- How to upload a file to another server using cURL and PHP?Best solution by Stack Overflow
- How to use Authorize Attribute in asp.net MVC?Best solution by Stack Overflow
- How to use angular.js with strongly typed view in asp.net-MVC?Best solution by ojdevelops.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.