Upload documents
This commit is contained in:
parent
3be0a28eb8
commit
b9e39cf57f
3 changed files with 43 additions and 1 deletions
|
@ -15,7 +15,7 @@ const (
|
|||
|
||||
func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
// TODO: when finish devel conver to global:
|
||||
var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html"))
|
||||
var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html", "upload.html"))
|
||||
|
||||
// TODO: use includes
|
||||
err := templates.ExecuteTemplate(w, "head.html", nil)
|
||||
|
|
38
upload.go
Normal file
38
upload.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"labix.org/v2/mgo"
|
||||
//"labix.org/v2/mgo/bson"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func storeFile(r *http.Request) {
|
||||
f, header, err := r.FormFile("epub")
|
||||
if err != nil {
|
||||
panic(err) // FIXME
|
||||
}
|
||||
defer f.Close()
|
||||
// FIXME: check the name exist
|
||||
fw, err := os.Create("new/" + header.Filename)
|
||||
if err != nil {
|
||||
panic(err) // FIXME
|
||||
}
|
||||
defer fw.Close()
|
||||
|
||||
const size = 1024
|
||||
var n int = size
|
||||
buff := make([]byte, size)
|
||||
for n == size {
|
||||
n, err = f.Read(buff)
|
||||
fw.Write(buff)
|
||||
}
|
||||
}
|
||||
|
||||
func uploadHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
storeFile(r)
|
||||
}
|
||||
|
||||
loadTemplate(w, "upload", nil)
|
||||
}
|
4
upload.html
Normal file
4
upload.html
Normal file
|
@ -0,0 +1,4 @@
|
|||
<form method="POST" enctype="multipart/form-data">
|
||||
<input accept="application/epub+zip" type="file" name="epub" />
|
||||
<input type="submit" />
|
||||
</form>
|
Reference in a new issue