Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ACS
Public
VILLASframework
VILLASweb-backend-go
Commits
0e54b7ba
Commit
0e54b7ba
authored
May 12, 2020
by
Sonja Happ
Browse files
simplify download function for files
parent
cb43c25c
Changes
1
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
routes/file/file_methods.go
View file @
0e54b7ba
...
...
@@ -22,12 +22,12 @@
package
file
import
(
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"mime/multipart"
"
os
"
"
net/http
"
"path/filepath"
"strconv"
"time"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
...
...
@@ -57,20 +57,16 @@ func (f *File) save() error {
func
(
f
*
File
)
download
(
c
*
gin
.
Context
)
error
{
err
:=
ioutil
.
WriteFile
(
f
.
Name
,
f
.
FileData
,
0644
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"file could not be temporarily created on server disk: %s"
,
err
.
Error
())
}
defer
os
.
Remove
(
f
.
Name
)
// create unique file name
filename
:=
"file_"
+
strconv
.
FormatUint
(
uint64
(
f
.
ID
),
10
)
+
"_"
+
f
.
Name
// detect the content type of the file
contentType
:=
http
.
DetectContentType
(
f
.
FileData
)
//Seems this headers needed for some browsers (for example without this headers Chrome will download files as txt)
c
.
Header
(
"Content-Description"
,
"File Transfer"
)
c
.
Header
(
"Content-Transfer-Encoding"
,
"binary"
)
c
.
Header
(
"Content-Disposition"
,
"attachment; filename="
+
f
.
Name
)
//c.Header("Content-Type", contentType)
c
.
File
(
f
.
Name
)
c
.
Header
(
"Content-Disposition"
,
"attachment; filename="
+
filename
)
c
.
Data
(
http
.
StatusOK
,
contentType
,
f
.
FileData
)
return
nil
}
func
(
f
*
File
)
register
(
fileHeader
*
multipart
.
FileHeader
,
objectType
string
,
objectID
uint
)
error
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment