fix: cannot upload in root

This commit is contained in:
sigoden 2022-05-28 20:40:49 +08:00
parent cdb7b5fc87
commit d9a917176a

View file

@ -45,20 +45,21 @@
class Uploader { class Uploader {
idx = 0; idx = 0;
file; file;
path;
$elem; $elem;
constructor(idx, file) { constructor(idx, file) {
this.idx = idx; this.idx = idx;
this.file = file; this.file = file;
this.path = location.pathname + "/" + file.name;
} }
upload() { upload() {
const { file, idx, path } = this; const { file, idx } = this;
let url = location.href.split('?')[0];
if (!url.endsWith("/")) url += "/";
url += encodeURI(file.name);
$uploaders.insertAdjacentHTML("beforeend", ` $uploaders.insertAdjacentHTML("beforeend", `
<div class="uploader path"> <div class="uploader path">
<div><svg height="16" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg></div> <div><svg height="16" viewBox="0 0 12 16" width="12"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg></div>
<a href="${path}" id="file${idx}">${file.name} (0%)</a> <a href="${url}" id="file${idx}">${file.name} (0%)</a>
</div>`); </div>`);
this.$elem = document.getElementById(`file${idx}`); this.$elem = document.getElementById(`file${idx}`);
@ -67,7 +68,7 @@
ajax.addEventListener("load", e => this.complete(e), false); ajax.addEventListener("load", e => this.complete(e), false);
ajax.addEventListener("error", e => this.fail(e), false); ajax.addEventListener("error", e => this.fail(e), false);
ajax.addEventListener("abort", e => this.fail(e), false); ajax.addEventListener("abort", e => this.fail(e), false);
ajax.open("PUT", path); ajax.open("PUT", url);
ajax.send(file); ajax.send(file);
} }