Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create and send a text file through XmlHttpRequest or $.ajax?
    text
    copied!<p>The server is expecting an html or txt file from a form with name: "websitetopdf". The file is created dynamically on client javascript and only have to work on Chrome.</p> <p>This is the form that should be sending it:</p> <pre><code>&lt;form action="send.php" method="post" enctype="multipart/form-data" name="websitetopdf" id="websitetopdf"&gt; &lt;input name="localfile" type="file" id="localfile" size="58"&gt; &lt;input name="filetype" type="hidden" id="filetype" value="IMG"&gt; &lt;input name="source" type="hidden" id="source" value="SOFT"&gt; &lt;input name="converternow" type="submit" id="converternow" style="width: 130px; font-weight: bold;" value="Convert to Image"&gt; &lt;/form&gt; </code></pre> <p>I want to directly send the file to send.php, I have tried this:</p> <pre><code>$("#sender").on("click", function(e) { var xhr = new XMLHttpRequest(); xhr.open("POST", "http://.../send.php", true); xhr.onreadystatechange = function() { if (xhr.readyState == 4) console.log(xhr.responseText); }; xhr.onload = function(e) { if (xhr.status == 200) console.log("uploaded"); //(correctly uploaded) else console.log("Error " + e.status + " occurred uploading your file.&lt;br \/&gt;"); }; var blob = new Blob(['abc123'], {type: 'text/plain'}); var formData = new FormData(); formData.append("localfile", blob); formData.append("filetype", "IMG"); formData.append("source", "SOFT"); xhr.send(formData); }); </code></pre> <p>I always get "<strong>it's not a valid file</strong>" from server<br></p>
 

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