File downloading

This commit is contained in:
Magnus Åhall 2023-06-22 17:42:34 +02:00
parent 86cedf9531
commit eaf5e2fb95
3 changed files with 14 additions and 8 deletions

12
main.go
View File

@ -20,9 +20,9 @@ import (
_ "embed"
)
const VERSION = "v0.1.4";
const VERSION = "v0.1.5";
const LISTEN_HOST = "0.0.0.0";
const DB_SCHEMA = 5
const DB_SCHEMA = 6
var (
flagPort int
@ -495,6 +495,10 @@ func nodeDownload(w http.ResponseWriter, r *http.Request) {// {{{
return
}
w.Header().Add("Content-Type", files[0].MIME)
w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, files[0].Filename))
w.Header().Add("Content-Length", strconv.Itoa(int(finfo.Size())))
read := 1
var buf []byte
for read > 0 {
@ -505,10 +509,6 @@ func nodeDownload(w http.ResponseWriter, r *http.Request) {// {{{
}
}
w.Header().Add("Content-Type", files[0].MIME)
w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, files[0].Filename))
w.Header().Add("Content-Length", strconv.Itoa(int(finfo.Size())))
}// }}}
func nodeFiles(w http.ResponseWriter, r *http.Request) {// {{{
var err error

5
sql/0006.sql Normal file
View File

@ -0,0 +1,5 @@
ALTER TABLE public.file DROP CONSTRAINT file_node_fk;
ALTER TABLE public.file ADD CONSTRAINT file_node_fk FOREIGN KEY (node_id) REFERENCES public.node(id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE public.file DROP CONSTRAINT file_fk;
ALTER TABLE public.file ADD CONSTRAINT file_user_fk FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -326,14 +326,15 @@ class Node {
}),
})
.then(response=>{
console.log(...response.headers);
let match = response.headers.get('content-disposition').match(/filename="([^"]*)"/)
fname = match[1]
return response.blob()
})
.then(blob=>{
let url = window.URL.createObjectURL(blob)
let a = document.createElement('a');
a.href = url;
a.download = "filename.xlsx";
a.download = fname;
document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
a.click();
a.remove(); //afterwards we remove the element again