fix: upload API only submit a task when files are selected

This commit is contained in:
DanyLE 2023-06-27 09:45:06 +02:00 committed by Dany LE
parent acd36a7a29
commit 6523fafe91

View File

@ -1306,20 +1306,22 @@ namespace OS {
* @returns {Promise<any>} * @returns {Promise<any>}
*/ */
export function upload(p: string, d: string): Promise<any> { export function upload(p: string, d: string): Promise<any> {
return API.Task(function (resolve, reject) { return new Promise( (resolve, reject) => {
//insert a temporal file selector //insert a temporal file selector
const o = const o =
$("<input>") $("<input>")
.attr("type", "file") .attr("type", "file")
.attr("multiple", "true"); .attr("multiple", "true");
o.on("change", function () { o.on("change", async () => {
try{
const files = (o[0] as HTMLInputElement).files; const files = (o[0] as HTMLInputElement).files;
const formd = new FormData(); const formd = new FormData();
formd.append("path", d); formd.append("path", d);
jQuery.each(files, (i, file) => { jQuery.each(files, (i, file) => {
formd.append(`upload-${i}`, file); formd.append(`upload-${i}`, file);
}); });
return $.ajax({ const ret = await API.Task((ok, nok) => {
$.ajax({
url: p, url: p,
data: formd, data: formd,
type: "POST", type: "POST",
@ -1327,13 +1329,20 @@ namespace OS {
processData: false, processData: false,
}) })
.done(function (data) { .done(function (data) {
resolve(data); ok(data);
}) })
.fail(function (j, s, e) { .fail(function (j, s, e) {
o.remove(); //o.remove();
reject(API.throwe(s)); nok(API.throwe(s));
}); });
}); });
resolve(ret);
}
catch(e)
{
reject(__e(e));
}
});
return o.trigger("click"); return o.trigger("click");
}); });
} }