Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://ajeeshms.github.io/articles/upload-files-using-ajax-in-asp-net-mvc/" rel="noreferrer">Upload files using AJAX in ASP.Net MVC</a></p> <p>Things have changed since HTML5</p> <p><strong>JavaScript</strong></p> <pre><code>document.getElementById('uploader').onsubmit = function () { var formdata = new FormData(); //FormData object var fileInput = document.getElementById('fileInput'); //Iterating through each files selected in fileInput for (i = 0; i &lt; fileInput.files.length; i++) { //Appending each file to FormData object formdata.append(fileInput.files[i].name, fileInput.files[i]); } //Creating an XMLHttpRequest and sending var xhr = new XMLHttpRequest(); xhr.open('POST', '/Home/Upload'); xhr.send(formdata); xhr.onreadystatechange = function () { if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) { alert(xhr.responseText); } } return false; } </code></pre> <p><strong>Controller</strong></p> <pre><code>public JsonResult Upload() { for (int i = 0; i &lt; Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; //Uploaded file //Use the following properties to get file's name, size and MIMEType int fileSize = file.ContentLength; string fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; //To save file, use SaveAs method file.SaveAs(Server.MapPath("~/")+ fileName ); //File will be saved in application root } return Json("Uploaded " + Request.Files.Count + " files"); } </code></pre> <p><strong><em>EDIT</em> : The HTML</strong></p> <pre><code>&lt;form id="uploader"&gt; &lt;input id="fileInput" type="file" multiple&gt; &lt;input type="submit" value="Upload file" /&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload