0

私は現在、ユーザーが写真をアップロードする Web アプリ プロジェクトに取り組んでいるgrailsの初心者です。ユーザーは、プロンプトのリストを使用して「ハント」を作成できます。各「プロンプト」は、参加者の目的です (例: お気に入りのキャンディーの写真をアップロードします)。基本的に、Web アプリは、写真家が自分の作品を社会的に共有するための「スカベンジャー ハント」ツールです。

現在、ユーザーがアップロードしたすべての写真を含む zip ファイルを生成する関数をユーザー コントローラーに書き込もうとして問題が発生しています。これは、現時点で私のコントローラー関数がどのように見えるかです。私が見つけたこの例を使用して開始しました。 zip ファイルの生成。

def downloadAlbum(){

    ByteArrayOutputStream baos = new ByteArrayOutputStream()
    ZipOutputStream zipFile = new ZipOutputStream(baos)

    //Instance of a user domain class
    def userInstance = User.findByLogin(auth.user())

    //pictures are uploaded to a prompt and stored as a photoInstance
    //this line of code gets the actual file stored as byte[]
        photoInstance.myFile = image.getBytes()

    //select all photos that belong to the user and store them into a list
    def photoInstanceList = userInstance ? Photo.findAllByMyUser(userInstance) : []


    //Mapping
    [userInstance: userInstance, photoInstanceList: photoInstanceList]      

      photoInstanceList.each {photo ->
        if (photoInstance.myFile != "") {
          File file = new File(photoInstance.myFile)
          zipFile.putNextEntry(new ZipEntry(Photo.title+".jpeg"))
          file.withInputStream { i ->

            zipFile << i

          }
          zipFile.closeEntry()
         }
        }
        zipFile.finish()
        response.setHeader("Content-disposition", "filename=\"${login}.zip\"")
        response.contentType = "application/zip"
        response.outputStream << baos.toByteArray()
        response.outputStream.flush()   

}

次に、このコードをユーザー ビューで使用して、ユーザー コントローラーで関数を呼び出すリンクを生成します。私は近いですか?コードに欠けている部分はありますか?

ところで、これは私が Stack Overflow で書いた最初の質問です。このエントリをお読みいただきありがとうございます。不明な点がある場合は、この質問を改善できるところを教えてください。私はgrails 2.1.1を使用しています

ありがとうございました!!

4

1 に答える 1