File 在Google应用程序引擎中处理Go中的图像和文本上传
关于如何在Go中处理文件上载,我遵循以下指南: 向下滚动至“完整示例应用程序”以查看整个代码 我试图用一个图像文件和一些文本来处理POST请求,但我似乎无法让它工作。我想将图像保存在Blobstore中,并将文本保存在我自己的结构中,作为它自己的类型“Item” 这是我的密码:File 在Google应用程序引擎中处理Go中的图像和文本上传,file,google-app-engine,post,go,File,Google App Engine,Post,Go,关于如何在Go中处理文件上载,我遵循以下指南: 向下滚动至“完整示例应用程序”以查看整个代码 我试图用一个图像文件和一些文本来处理POST请求,但我似乎无法让它工作。我想将图像保存在Blobstore中,并将文本保存在我自己的结构中,作为它自己的类型“Item” 这是我的密码: func handleUpload(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) blobs, _, err :
func handleUpload(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
blobs, _, err := blobstore.ParseUpload(r)
if err != nil {
serveError(ctx, w, err)
return
}
file := blobs["file"]
if len(file) == 0 {
log.Errorf(ctx, "no file uploaded")
http.Redirect(w, r, "/", http.StatusFound)
return
}
var n string = r.FormValue("name")
lat, errLat := strconv.ParseFloat(r.FormValue("latitude"), 64)
long, errLong := strconv.ParseFloat(r.FormValue("longitude"), 64)
if errLat != nil {
http.Error(w, "Invalid latitude format: " + errLat.Error(), http.StatusInternalServerError)
}
if errLong != nil {
http.Error(w, "Invalid longitude format: " + errLong.Error(), http.StatusInternalServerError)
}
c := appengine.NewContext(r)
g := Item { Name: n, Date: time.Now(), Latitude: lat, Longitude: long, ImageKey: string(file[0].BlobKey)}
// We set the same parent key on every Greeting entity to ensure each Greeting
// is in the same entity group. Queries across the single entity group
// will be consistent. However, the write rate to a single entity group
// should be limited to ~1/second.
key := datastore.NewIncompleteKey(c, "Item", itemKey(c))
_, err = datastore.Put(c, key, &g)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/serve/?blobKey="+string(file[0].BlobKey), http.StatusFound)
}
当我开始从中的帖子中检索文本时
var n string = r.FormValue("name")
文本为空,因为它已在保存图像的请求中使用。我知道一个请求只能“使用”一次
我的问题是:我是不是走错了路?如何在Go中从POST请求中获取文本和图像
以下是HTML表单:
<html>
<body>
<form action="{{.}}" method="POST" enctype="multipart/form-data">
Name: <br>
<div><textarea name="name" rows="1" cols="60"></textarea></div>
Latitude: <br>
<div><textarea name="latitude" rows="1" cols="60"></textarea></div>
longitude: <br>
<div><textarea name="longitude" rows="1" cols="60"></textarea></div>
Upload File: <input type="file" name="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
名称:
纬度:
经度:
上传文件:
请向我们展示您的HTML
。添加了HTML,顺便说一句,图像成功保存在Blobstore中。@PæturMagnussen您认为这个问题完美吗!这解决了我的问题。非常感谢,@mlkcode请向我们展示您的HTML
。添加了HTML,顺便说一句,图像成功保存在Blobstore中。@PæturMagnussen您看到这个问题了吗!这解决了我的问题。非常感谢,@MlkCode