doulang1945 2013-06-27 21:33
浏览 39
已采纳

寻找一种使用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!

  • 写回答

2条回答 默认 最新

  • duanchen7401 2013-06-28 01:32
    关注

    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)
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题