0
votes

I am currently working on a project written in Go for Google App Engine. I have problem with uploading file/image to blobstore without having to reload the site.

So this is the html form:

<form id="file-form" action="{{.UploadUrl}}" method="POST" enctype="multipart/form-data">
    <fieldset style="width: 100%;" data-uk-margin>
        <div class="uk-form-row">
            <div id="fields"></div>
        </div>

        <div class="uk-form-row" style="padding-top: 5px; padding-bottom: 5px;">
            <textarea id="textArea" cols="" rows="10" name="description" placeholder="Description"></textarea>
        </div>                      
        <div class="uk-form-row" style="padding-top: 5px; padding-bottom: 5px;">
            <p>Upload File: </p> <input id="file-select" type="file" name="file-select" accept="image/png">
        </div>                              
        <div class="uk-form-row" style="opacity: 1.0;">
            <div class="uk-flex uk-flex-center" id="buttonDiv">
               <button type="submit" id="submitButton" class="uk-button uk-button-primary">Upload</button>
            </div>
        </div>
    </fieldset>
</form>

And javascript code to upload the image:

var form = document.getElementById("file-form");
            var fileSelect = document.getElementById("file-select");
            var uploadButton = document.getElementById("submitButton");
            var description = document.getElementById("textArea");

            form.onsubmit = function(event) {
                event.preventDefault();

                uploadButton.innerHTML = "Uploading...";

                if (fileSelect.files.length == 0) {
                    alert("No images selected");
                    uploadButton.innerHTML = "Upload";
                    return;
                }

                var file = fileSelect.files[0];
                console.log(file.name);
                var formData = new FormData();
                formData.append("file", file);
                formData.append("description", description.value);

                var xhr = new XMLHttpRequest();
                xhr.open('POST', "/api/files/fileUpload", true);

                xhr.onload = function() {
                    if (xhr.status === 200) {
                        uploadButton.innerHTML = "Upload"
                    } else {
                        alert("An error occurred!");
                    }
                }
                xhr.send(formData);
            }

Go method to receive file:

c := appengine.NewContext(r)
    u := user.Current(c)

    if u == nil {
        url, err := user.LoginURL(c, r.URL.String())
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        w.Header().Set("Location", url)
        w.WriteHeader(http.StatusFound)
        return
    }

    logoutUrl, e := user.LogoutURL(c, "/redirect")
    if e != nil {
        panic(e)
    }
    email := u.Email

    uploadURL, error := blobstore.UploadURL(c, "/api/files/fileUpload", nil)
    if error != nil {
        panic(error)
    }

    data := WebpageData{LogoutUrl: logoutUrl, UserName: email, UploadUrl: uploadURL}

    template := template.Must(template.New("template").Parse(fileValue("./console/page/newForm.html")))

    err := template.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }

And finally the errors that bother me: 1. from js console in browser:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)
  1. from go console/log:

    2015/04/13 15:10:15 http: panic serving 127.0.0.1:49543: mime: no media type goroutine 16 [running]: net/http.func·011() /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1130 +0xbb api.check(0xb0db00, 0xc20800a970) api/api.go:40 +0x50 api.fileUploadHandler(0xb136e0, 0xc208045540, 0xc2080f1d40) api/files.go:19 +0x8a net/http.HandlerFunc.ServeHTTP(0x5a2088, 0xb136e0, 0xc208045540, 0xc2080f1d40) /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1265 +0x41 net/http.(*ServeMux).ServeHTTP(0xc20803a690, 0xb136e0, 0xc208045540, 0xc2080f1d40) /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1541 +0x17d appengine_internal.handleFilteredHTTP(0xb136e0, 0xc208045540, 0xc2080f1d40) /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/appengine_internal/api_dev.go:98 +0x413 net/http.HandlerFunc.ServeHTTP(0x5a20f8, 0xb136e0, 0xc208045540, 0xc2080f1d40) /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1265 +0x41 net/http.serverHandler.ServeHTTP(0xc208042120, 0xb136e0, 0xc208045540, 0xc2080f1d40) /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1703 +0x19a net/http.(*conn).serve(0xc2080454a0) /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1204 +0xb57 created by net/http.(*Server).Serve /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1751 +0x35e

Any ideas how to tackle this problem. Thanks in advance :)

1
Have you considered setting the Content-Type on the XHR request a la code.google.com/p/go-socketio/source/browse/socket.io-client/…Momer

1 Answers

-1
votes

Blobstore is deprecated: https://cloud.google.com/appengine/docs/go/blobstore/reference. Here's how you can upload files to GCS:

import (
    // these are the appengine imports
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "google.golang.org/appengine"
    "google.golang.org/appengine/datastore"
    "google.golang.org/appengine/file"
    "google.golang.org/appengine/urlfetch"
    "google.golang.org/cloud"
    "google.golang.org/cloud/storage"
)

func serveFilesUpload(res http.ResponseWriter, req *http.Request) {
    ctx := appengine.NewContext(req)

    // every app engine app comes with a default bucket
    bucket, err := file.DefaultBucketName(ctx)
    if err != nil {
        panic(err)
    }

    // we can't use the regular http client because we need
    // oauth headers added, and also need to use urlfetch
    // for app engine
    hc := &http.Client{
        Transport: &oauth2.Transport{
            Source: google.AppEngineTokenSource(ctx, storage.ScopeFullControl),
            Base:   &urlfetch.Transport{Context: ctx},
        },
    }

    // I'm just giving files random names
    id := uuid.NewRandom().String()

    // this get's the file out of the multipart form
    ff, _, err := req.FormFile("file")
    if err != nil {
        panic(err)
    }
    defer ff.Close()

    cctx := cloud.NewContext(appengine.AppID(ctx), hc)
    wc := storage.NewWriter(cctx, bucket, id)
    io.Copy(wc, ff)
    // normally you'd just defer the .Close, but in this case
    // we care about the error it returns
    err = wc.Close()
    if err != nil {
        panic(err)
    }

    json.NewEncoder(res).Encode(id)
}

A complete example is available here: https://github.com/calebdoxsey/tutorials/tree/master/appengine/cms.