2013-02-16 1 views
0

GAE 앱에 파일을 업로드하려고합니다. Go를 사용하고 r.FormValue()을 사용하여 Google App Engine에서 파일을 업로드하려면 어떻게해야합니까?GAE에 파일 업로드

+2

을 시도한 것을 볼 것이 좋을 것이다. –

답변

3

중간 반환 매개 변수 인 "기타"를 사용하여 문제를 해결할 수있었습니다. 다음이 코드는

blobs, other, err := blobstore.ParseUpload(r) 

그런 다음 formkey에게

file := blobs["file"] 
**name := other["name"]** //name is a form field 
**description := other["description"]** //descriptionis a form field 

해당 할당하고 내 구조체의 값 할당이 같은이가있다

newData := data{ 
    Name: **string(name[0])**, 
    Description: **string(description[0])**, 
    Image: string(file[0].BlobKey),   
} 

datastore.Put(c, datastore.NewIncompleteKey(c, "data", nil), &newData) 

100 % 확인을 사용하는 업로드 핸들러 내부에 옳은 일이지만 내 문제를 해결하고 이미지를 BLOBSTORE에 업로드하고 다른 데이터와 BLOB 키를 데이터 저장소에 저장합니다.

다른 사람들에게도 도움이 될 수 있기를 바랍니다.

+0

** **는 코드의 일부가 아닌 강조만을 의미합니다. – sagit

4

아이디어를 얻으려면 Blobstore Go API Overview을 사용해야하며 &을 Google App Engine에 Go를 사용하여 저장하는 방법은 full example입니다.

완전히 별도의 응용 프로그램에서이 예제를 수행 할 것을 제안합니다. 따라서 기존 예제와 통합하기 전에 잠깐 실험 해 볼 수 있습니다.

0

여기에서 전체 예제를 시도했습니다. https://developers.google.com/appengine/docs/go/blobstore/overview, 그리고 blobstore에서 업로드를 수행하고 잘 처리했습니다.

그러나 데이터 저장소의 어딘가에 저장 될 추가 게시물 값을 삽입하면 "r.FormValue()"값이 지워 집니까? 아래 코드를 참조하십시오.

func handleUpload(w http.ResponseWriter, r *http.Request) { 
     c := appengine.NewContext(r) 

     //tried to put the saving in the datastore here, it saves as expected with correct values but would raised a server error. 

     blobs, _, err := blobstore.ParseUpload(r) 
     if err != nil { 
       serveError(c, w, err) 
       return 
     } 
     file := blobs["file"] 
     if len(file) == 0 { 
       c.Errorf("no file uploaded") 
       http.Redirect(w, r, "/", http.StatusFound) 
       return 
     } 

     // a new row is inserted but no values in column name and description 
     newData:= data{ 
      Name: r.FormValue("name"), //this is always blank 
      Description: r.FormValue("description"), //this is always blank 
     } 

     datastore.Put(c, datastore.NewIncompleteKey(c, "Data", nil), &newData) 

     //the image is displayed as expected 
     http.Redirect(w, r, "/serve/?blobKey="+string(file[0].BlobKey), http.StatusFound) 
} 

일반 데이터와 업로드를 결합 할 수 있습니까? r.FormValue()의 값이 파일 (입력 파일 유형)을 제외하고 사라지는 것처럼 보이는 이유는 무엇입니까? 업로드 결과로 blobkey를 다른 데이터에 연결하기 전에 먼저 업로드를 강제해야만한다고해도 업로드 처리기에 r.FormValue()를 전달할 수 없으므로 불가능합니다 (이는 내가 말했던 것처럼 비어 있거나, blob, _, err : = blobstore.ParseUpload (r) 문)보다 먼저 액세스 할 때 오류가 발생합니다. 누군가가이 문제를 해결할 수 있기를 바랍니다. 고맙습니다!

0

Blobstore API를 사용하는 것 외에도 Request.FormFile() 메서드를 사용하여 파일 업로드 콘텐츠를 가져올 수 있습니다. 추가 도움말을 보려면 net\http 패키지 설명서를 사용하십시오.

요청을 직접 사용하면 업로드 POST 메시지를 처리하기 전에 blobstore.UploadUrl() 설정을 건너 뛸 수 있습니다.

간단한 예는 다음과 같습니다

func uploadHandler(w http.ResponseWriter, r *http.Request) { 
    // Create an App Engine context. 
    c := appengine.NewContext(r) 

    // use FormFile() 
    f, _, err := r.FormFile("file") 
    if err != nil { 
      c.Errorf("FormFile error: %v", err) 
      return 
    } 
    defer f.Close() 

    // do something with the file here 
    c.Infof("Hey!!! got a file: %v", f) 
}