寻找一种使用Ajax使用angularjs将文件上传到Appengine Go服务器的方法

Javascript code:

$http({
    method: 'POST',
    url: 'some/path',
    headers: { 'Content-Type': false },
    transformRequest: function (data) {
      var formData = new FormData();
      formData.append("file", data);
      return formData;
    },
    data: file
}).
success(function (data, status, headers, config) {
    alert("success!");
}).
error(function (data, status, headers, config) {
    alert("failed!");
});

Server code:

func addImageHandler(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    blobs, _, err := blobstore.ParseUpload(r)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    file := blobs["file"]
    ....
}

I'm hitting the handler just fine but when i parse the upload I don't have any files in my blobs map. Is there something obvious i'm missing?

Thanks!

The problem has turned out to be a detail that wasn't included in the original question. You need to ensure that you're using the blobstore to generate the upload url. For example:

func ServeUploadUrl(w http.ResponseWriter, request *http.Request) {
    context := appengine.NewContext(request)
    myUrl := "some/upload/url"

    // create the blobstore url here
    blobstore.UploadURL(context, myUrl, nil)
    ...
}

Seems like your Content-Type is wrong, it should be multipart-form etc.

headers: { 'Content-Type': false },

Make sure the form has a Field type like:

<input type="file" />

Finally make sure that

len(blobs["file"]) != 0

If it is then it most likely means that the browser is not sending the data in the right format and appengine is not recognizing it. Use Chrome developer tools to inspect what is the ajax call trying to send.

This is a fiddle for a nice angularjs widget to upload data: http://jsfiddle.net/CPsRs/