Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript file upload not working in Chrome
    primarykey
    data
    text
    <p>I have implemented the following JS code to upload a file. In IE it works fine, but in chrome I get an empty file on the server side. I looked at the request data using Fiddler and found that the request doesn't contain the file data in Chrome (in IE it does). It looks like this:</p> <pre><code>----WebkitFormBoundary4kjth5kjt54h Content-Disposition: form-data; name="C:\fakepath\file.txt"; filename="" Content-Type: application/octet-stream ----WebkitFormBoundary4kjth5kjt54h-- </code></pre> <p>And in IE it looks like:</p> <pre><code>----------------7dd435hb8d7gs34 Content-Disposition: form-data; name="C:\fakepath\file.txt"; filename="file.txt" Content-Type: text/plain The file data bla bla bla ----------------7dd435hb8d7gs34-- </code></pre> <p>So there must be a problem on the client side, but I can't figure out what it is.</p> <p>This code is being activated when the user clicks a button.</p> <p>Please don't suggest creating a regular HTML <code>&lt;form&gt;</code> tag, because my case doesn't make it possible. It has to be done using javascript.</p> <pre><code>var form = document.createElement("FORM"); form.method = "post"; form.style.display = "none"; // adding some input elements to the form... // ... // adding the file input form.enctype = "multipart/form-data"; var fileInputs = jQuery("input:file"); fileInputs.each(function() { if (this.value != "") { form.appendChild(this); } }); form.action = "my.servlet.path"; form.submit(); </code></pre> <p><strong>EDIT:</strong> New info: I noticed in chrome devtools in the Network tab that the request status is "cancelled". What does that mean? The request <em>does</em> reach the server, so what was cancelled? And why?</p> <hr> <h3>Solved</h3> <p>Ok, I found the bug. The code I posted here is a simlified version of what's actually going on. What really happened was there was an update of some global data on the page before the <code>form.submit()</code>, and KnockOut picked up on that update and updated the <code>&lt;input type="file"&gt;</code> with an empty value. And then obviously the file was not sent to the server.</p> <p>After changing that, it works fine, but the request still shows up as cancelled in the Network tab. Don't know why...</p>
    singulars
    1. This table or related slice is empty.
    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. 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