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" _ "embed"
) )
const VERSION = "v0.1.4"; const VERSION = "v0.1.5";
const LISTEN_HOST = "0.0.0.0"; const LISTEN_HOST = "0.0.0.0";
const DB_SCHEMA = 5 const DB_SCHEMA = 6
var ( var (
flagPort int flagPort int
@ -495,6 +495,10 @@ func nodeDownload(w http.ResponseWriter, r *http.Request) {// {{{
return 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 read := 1
var buf []byte var buf []byte
for read > 0 { 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) {// {{{ func nodeFiles(w http.ResponseWriter, r *http.Request) {// {{{
var err error 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=>{ .then(response=>{
console.log(...response.headers); let match = response.headers.get('content-disposition').match(/filename="([^"]*)"/)
fname = match[1]
return response.blob() return response.blob()
}) })
.then(blob=>{ .then(blob=>{
let url = window.URL.createObjectURL(blob) let url = window.URL.createObjectURL(blob)
let a = document.createElement('a'); let a = document.createElement('a');
a.href = url; 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 document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
a.click(); a.click();
a.remove(); //afterwards we remove the element again a.remove(); //afterwards we remove the element again