upload and drag n drop upades

This commit is contained in:
Henrique Dias
2016-06-23 09:15:00 +01:00
parent 1e3ec14e92
commit d66bce3e6d
3 changed files with 88 additions and 81 deletions

View File

@@ -9,6 +9,7 @@ package filemanager
import (
"io"
"log"
"mime"
"mime/multipart"
"net/http"
@@ -135,25 +136,27 @@ func Upload(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
log.Println(err)
return http.StatusInternalServerError, err
}
// For each file header in the multipart form
for _, fheaders := range r.MultipartForm.File {
for _, headers := range r.MultipartForm.File {
// Handle each file
for _, hdr := range fheaders {
for _, header := range headers {
// Open the first file
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
var src multipart.File
if src, err = header.Open(); nil != err {
return http.StatusInternalServerError, err
}
filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
filename = filename + header.Filename
filename = filepath.Clean(filename)
// Create the file
var outfile *os.File
if outfile, err = os.Create(filename); nil != err {
var dst *os.File
if dst, err = os.Create(filename); nil != err {
if os.IsExist(err) {
return http.StatusConflict, err
}
@@ -161,11 +164,11 @@ func Upload(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
}
// Copy the file content
if _, err = io.Copy(outfile, infile); nil != err {
if _, err = io.Copy(dst, src); nil != err {
return http.StatusInternalServerError, err
}
defer outfile.Close()
defer dst.Close()
}
}