Skip to content

Commit 5f3c771

Browse files
committed
Use POST form instead of XHR
This allows to send files without having to configure CORS.
1 parent e023386 commit 5f3c771

File tree

3 files changed

+19
-53
lines changed

3 files changed

+19
-53
lines changed

calibration.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,14 @@ <h2>Interpreting Results:</h2>
11791179
<iframe loading="lazy" width="640" height="480" src="https://www.youtube.com/embed/CZC5n2J8k_c" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
11801180
<p>You may wish to stop the print early if it is clear extrusion has ceased or become unreliable. This may prevent the need for disassembly of the extruder to clear the blockage.</p>
11811181
</div>
1182+
1183+
<form style="display: none" name="octoprintForm" id="octoprintForm" action="#" method="post" enctype="multipart/form-data" target="_blank">
1184+
<input type="file" name="file"/>
1185+
<input type="hidden" name="apikey" value=""/>
1186+
<input type="hidden" name="path" value="TeachingTechYT"/>
1187+
<input type="hidden" name="select" value="true"/>
1188+
<input type="hidden" name="print" value="true"/>
1189+
</form>
11821190
</div>
11831191
<div id="up"></div>
11841192
<div id="footer"></div>

js/createform.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,8 @@ var downloadGcodeHtml = /*html*/ `<h4>Download</h4>
471471
<div class="octoprintExp">
472472
<label>Configure Octoprint:</label>
473473
<ul>
474-
<li>Get API Key go to <b>User Settings > Application Keys</b> and manually generate new application key.</li>
475-
<li>Enable CORS support by going to <b>OctoPrint Settings > API</b> and checking <b>Allow Cross Origin Resource Sharing (CORS)</b>. Restart afterwards</li>
476-
<li>Restart afterwards</li>
477-
</ul>
478-
479-
<label>Configure Moonraker if used with Klipper:</label>
480-
<ul>
481-
<li>As part of the <a href="https://moonraker.readthedocs.io/en/latest/configuration/#authorization">[authorization] config</a>
482-
add to <b>cors_domains:</b> the <b><script>document.write(window.location.origin);</script></b>.</li>
483-
<li>Restart afterwards</li>
474+
<li>Login to your Octoprint from this browser</li>
475+
<li>Or get API Key from <b>User Settings > Application Keys</b></li>
484476
</ul>
485477
</div>
486478

js/gcodeprocessing.js

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -739,54 +739,20 @@ function uploadGcode(form, fileName) {
739739

740740
// get only basename
741741
if (!fileName) {
742-
fileName = form.description.value + ".gcode";
742+
fileName = form.description.value;
743743
}
744+
fileName += ".gcode";
744745
fileName = fileName.split('/').reverse()[0];
745746

746747
// remove `/` from the end of URL
747-
var url = form.octoprint_url.value.replace(/\/$/, '');
748+
var url = form.octoprint_url.value.replace(/\/+$/, '');
748749
url += "/api/files/local";
749750

750-
// this detects a `mixed-context` scenario:
751-
// sending request to `http:` when connected via `https:` is not possible
752-
if (window.location.protocol == "https:" && url.toLowerCase().startsWith("http:")) {
753-
httpUrl = window.location.href.replace('https:', 'http:');
754-
755-
alert("Your local Octoprint/Moonraker uses `http://`. " +
756-
"You need to open the `" + httpUrl + "` instead");
757-
return;
758-
}
759-
760-
const formData = new FormData();
761-
formData.append("file", new Blob([output], {type : 'text/plain'}), fileName);
762-
formData.append("path", "TeachingTechYT");
763-
formData.append("select", "true");
764-
formData.append("print", "true");
751+
const dataTransfer = new DataTransfer();
752+
dataTransfer.items.add(new File([output], fileName, {type : 'text/plain'}));
765753

766-
fetch(url, {
767-
method: "POST",
768-
body: formData,
769-
headers: {
770-
'X-Api-Key': form.octoprint_key.value
771-
}
772-
})
773-
.then(response => {
774-
if (response.ok) {
775-
response.json().then(data => {
776-
if (data.print_started) {
777-
alert("Successfully uploaded and started print from " + fileName);
778-
} else {
779-
alert("Successfully uploaded, but print was not started from " + fileName);
780-
}
781-
});
782-
} else {
783-
alert("Failed to upload due to " + response.statusText);
784-
}
785-
})
786-
.catch((error) => {
787-
alert("Failed to upload due to an error. Possible causes:\n" +
788-
"- CORS not configured properly\n" +
789-
"- url does not start with `http://` or `https://`\n" +
790-
"\n" + error);
791-
});
754+
document.octoprintForm.action = url;
755+
document.octoprintForm.file.files = dataTransfer.files;
756+
document.octoprintForm.apikey.value = form.octoprint_key.value;
757+
document.octoprintForm.submit();
792758
}

0 commit comments

Comments
 (0)