Skip to content

Commit 2ca0c46

Browse files
committed
Use POST form instead of XHR
This allows to send files without having to configure CORS.
1 parent 1a1cf38 commit 2ca0c46

File tree

3 files changed

+16
-50
lines changed

3 files changed

+16
-50
lines changed

calibration.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,14 @@ <h2>Fixing persistent dimensional accuracy after X/Y/Z steps per unit have been
10341034
<p>One final measure, that is the least desirable, is to design parts to be printed bigger or smaller to compensate. This is a band aid approach and falls apart very quickly once we print geometry designed by other people.</p>
10351035

10361036
</div>
1037+
1038+
<form style="display: none" name="octoprintForm" id="octoprintForm" action="#" method="post" enctype="multipart/form-data" target="_blank">
1039+
<input type="file" name="file"/>
1040+
<input type="hidden" name="apikey" value=""/>
1041+
<input type="hidden" name="path" value="TeachingTechYT"/>
1042+
<input type="hidden" name="select" value="true"/>
1043+
<input type="hidden" name="print" value="true"/>
1044+
</form>
10371045
</div>
10381046
<div id="up"></div>
10391047
<div id="footer"></div>

js/createform.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,8 @@ var downloadGcodeHtml = /*html*/ `<h4>Download</h4>
340340
<div class="octoprintExp">
341341
<label>Configure Octoprint:</label>
342342
<ul>
343-
<li>Get API Key go to <b>User Settings > Application Keys</b> and manually generate new application key.</li>
344-
<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>
345-
<li>Restart afterwards</li>
346-
</ul>
347-
348-
<label>Configure Moonraker if used with Klipper:</label>
349-
<ul>
350-
<li>As part of the <a href="https://moonraker.readthedocs.io/en/latest/configuration/#authorization">[authorization] config</a>
351-
add to <b>cors_domains:</b> the <b><script>document.write(window.location.origin);</script></b>.</li>
343+
<li>Login to your Octoprint</li>
344+
<li>or get API Key go to <b>User Settings > Application Keys</b> and manually generate new application key.</li>
352345
<li>Restart afterwards</li>
353346
</ul>
354347
</div>

js/gcodeprocessing.js

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -678,46 +678,11 @@ function uploadGcode(form, fileName) {
678678
var url = form.octoprint_url.value.replace(/\/$/, '');
679679
url += "/api/files/local";
680680

681-
// this detects a `mixed-context` scenario:
682-
// sending request to `http:` when connected via `https:` is not possible
683-
if (window.location.protocol == "https:" && url.toLowerCase().startsWith("http:")) {
684-
httpUrl = window.location.href.replace('https:', 'http:');
681+
const dataTransfer = new DataTransfer();
682+
dataTransfer.items.add(new File([output], fileName, {type : 'text/plain'}));
685683

686-
alert("Your local Octoprint/Moonraker uses `http://`. " +
687-
"You need to open the `" + httpUrl + "` instead");
688-
return;
689-
}
690-
691-
const formData = new FormData();
692-
formData.append("file", new Blob([output], {type : 'text/plain'}), fileName);
693-
formData.append("path", "TeachingTechYT");
694-
formData.append("select", "true");
695-
formData.append("print", "true");
696-
697-
fetch(url, {
698-
method: "POST",
699-
body: formData,
700-
headers: {
701-
'X-Api-Key': form.octoprint_key.value
702-
}
703-
})
704-
.then(response => {
705-
if (response.ok) {
706-
response.json().then(data => {
707-
if (data.print_started) {
708-
alert("Successfully uploaded and started print from " + fileName);
709-
} else {
710-
alert("Successfully uploaded, but print was not started from " + fileName);
711-
}
712-
});
713-
} else {
714-
alert("Failed to upload due to " + response.statusText);
715-
}
716-
})
717-
.catch((error) => {
718-
alert("Failed to upload due to an error. Possible causes:\n" +
719-
"- CORS not configured properly\n" +
720-
"- url does not start with `http://` or `https://`\n" +
721-
"\n" + error);
722-
});
684+
document.octoprintForm.action = url;
685+
document.octoprintForm.file.files = dataTransfer.files;
686+
document.octoprintForm.apikey.value = form.octoprint_key.value;
687+
document.octoprintForm.submit();
723688
}

0 commit comments

Comments
 (0)