0

最初に、ファイルをアップロードするための Javascript 関数を実装しようとしましたが、ファイルがアップロードされず、そのコードで遊ぶ方法がわかりませんでした。

これは「ビューファイル」です:

<div id="file-uploader">

    <script>
        function createUploader(){
            var uploader = new qq.FileUploader({
                element: document.getElementById('file-uploader'),
                action: '/upload',
                debug: true
            });
        }
        // in your app create uploader as soon as the DOM is ready
        // don't wait for the window to load
        window.onload = createUploader;
    </script>    

</div>

そしてこれはコントローラーです:

package controllers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import models.Photo;
import models.User;
import play.*;
import play.mvc.*;

import org.apache.commons.io.IOUtils;

public class Uploader extends Controller {

    public static Long id;

    public static void index(Long id) {
        System.out.println("this is id inside the tester"+id);
        new Uploader(id);
        render();
    }

    public Uploader(Long id) {
        this.id = id;
    }

    public static void upload(String qqfile) {

        if (request.isNew) {

            FileOutputStream moveTo = null;

            Logger.info("Name of the file %s", qqfile);
            // Another way I used to grab the name of the file
            String filename = request.headers.get("x-file-name").value();

            Logger.info("Absolute on where to send %s", Play.getFile("").getAbsolutePath() + File.separator + "uploads" + File.separator);
            try {

                String filelocation = File.separator + "uploads" + File.separator + filename;

                InputStream data = request.body;

                moveTo = new FileOutputStream(new File(Play.getFile("").getAbsolutePath())
                        + File.separator + "uploads" + File.separator + filename);

                IOUtils.copy(data, moveTo);

                Users.show(id);

            } catch (Exception ex) {

                // catch file exception
                // catch IO Exception later on
                renderJSON("{success: false}");
                System.out.println("Exception is:" + ex);
            }

        }

    }
}

カスタマイズしたいのですが (たとえば、アップロード関数にさらにパラメーターを渡すなど)、それを行う方法が見つかりません...

どこが間違っているのか教えてください。可能であれば、フォームからファイルをアップロードする方法を教えてもらえますか?

4

1 に答える 1

0

http://www.playframework.org/documentation/1.0/5things

ポイント5:

HTMLフォーム:

<form action="@{Article.uploadPhoto()}" method="POST" enctype="multipart/form-data">
    <input type="text" name="title" />
    <input type="file" id="photo" name="photo" />
    <input type="submit" value="Send it..." />
</form>

そしてJavaコード:

public static void uploadPhoto(String title, File photo) {
   ...
}
于 2012-05-21T15:11:51.923 に答える