This commit is contained in:
Henrique Dias 2015-09-26 12:34:53 +01:00
parent 5316686d2d
commit cc31d89d6c
4 changed files with 92 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@ -136,6 +136,52 @@ $(document).on('ready pjax:success', function() {
return false;
}
});
$("#upload").click(function(event) {
event.preventDefault();
$('.actions input[type="file"]').click();
});
$('input[type="file"]').on('change', function(event) {
event.preventDefault();
files = event.target.files;
// Create a formdata object and add the files
var data = new FormData();
$.each(files, function(key, value) {
data.append(key, value);
});
$.ajax({
url: window.location.pathname,
type: 'POST',
data: data,
cache: false,
dataType: 'json',
headers: {
'X-Upload': 'true',
},
processData: false,
contentType: false,
}).done(function(data) {
notification({
text: "File(s) uploaded successfully.",
type: 'success',
timeout: 5000
});
$.pjax({
url: window.location.pathname,
container: '#content'
})
}).fail(function(data) {
notification({
text: 'Something went wrong.',
type: 'error'
});
console.log(data);
});
});
}
// If it's editor page

View File

@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"errors"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
@ -65,6 +67,48 @@ func delete(w http.ResponseWriter, r *http.Request) (int, error) {
func post(w http.ResponseWriter, r *http.Request) (int, error) {
// Remove both beginning slashes
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
// If it's the upload of a file
if r.Header.Get("X-Upload") == "true" {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
w.Write([]byte(err.Error()))
return 500, err
}
// For each file header in the multipart form
for _, fheaders := range r.MultipartForm.File {
// Handle each file
for _, hdr := range fheaders {
// Open the first file
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
// Create the file
var outfile *os.File
if outfile, err = os.Create(r.URL.Path + hdr.Filename); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
// Copy the file content
if _, err = io.Copy(outfile, infile); nil != err {
w.Write([]byte(err.Error()))
return 500, err
}
}
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return 200, nil
}
// Get the JSON information sent using a buffer
buffer := new(bytes.Buffer)
buffer.ReadFrom(r.Body)

View File

@ -12,7 +12,7 @@
<div class="container">
{{if .CanGoUp}}<a data-pjax href=".." class="up" title="Up one level"><i class="fa fa-arrow-left fa-lg"></i></a>{{end}}
<div class="go-right">
<input type="file" value="Upload">
<input type="file" value="Upload" multiple>
<button id="upload">Upload <i class="fa fa-cloud-upload"></i></button>
<button class="default new">New <i class="fa fa-plus"></i></button>
<div id="new-file">