Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a <a href="https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Example.3A_Uploading_a_user-selected_file" rel="nofollow"><code>guide</code></a> about the File API that could be used to upload files. But don't forget that this won't work on IE9. If you need to support such browsers you could use hidden iframes to simulate uploading files with AJAX. That's why plugins such as <a href="http://www.malsup.com/jquery/form/" rel="nofollow">jquery.form</a> exist. In order to make it a single line of code so that you don't have to worry about browser support and stuff:</p> <pre><code>if ($Form.valid()) { // get the url and the form data to send the request $Form.ajaxSubmit({ success: function (data, textStatus, jqXHR) { // do something here }, error: function (jqXHR, textStatus, errorThrown) { // do something here } }); } </code></pre> <hr> <p>UPDATE:</p> <p>As requested in the comments section here's how you could use the File API. </p> <p>Let's suppose that you have the following form:</p> <pre><code>@using (Html.BeginForm(MVC.EditImageReceiptLine(), null, FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.HiddenFor(model =&gt; model.ReceiptLineSetID) @Html.HiddenFor(model =&gt; model.LineNumber) &lt;input id="uploadFile" name="uploadFile" type="file" value="Choose New Image" /&gt; @Html.LabelFor(model =&gt; model.ImageDescription) @Html.TextBoxFor(model =&gt; model.ImageDescription) } </code></pre> <p>and some link that will trigger the form submission:</p> <pre><code>&lt;a href="#" id="upload"&gt;Upload the file using HTML5 File API&lt;/a&gt; </code></pre> <p>Now in a js file you could have the following:</p> <pre><code>$('#upload').click(function () { if (!window.FileReader) { // the browser doesn't support the File API - there's no need // to continue; alert('To use this functionality please use a modern browser'); return; } var $form = $('form'); var uri = $form.action; var xhr = new XMLHttpRequest(); var fd = new FormData(); $form.find(':input').each(function () { if ($(this).is('input[type="file"]')) { var files = $(this)[0].files; if (files.length &gt; 0) { fd.append(this.name, files[0]); } } else { fd.append(this.name, $(this).val()); } }); xhr.open('POST', uri, true); xhr.onreadystatechange = function () { if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) { // handle response. alert(xhr.responseText); } }; xhr.send(fd); }); </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.
 

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