Upload documents

This commit is contained in:
Las Zenow 2012-08-15 11:40:19 +02:00
parent 3be0a28eb8
commit b9e39cf57f
3 changed files with 43 additions and 1 deletions

View file

@ -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
View 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
View 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>