最初に、ファイルをアップロードするための 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);
}
}
}
}
カスタマイズしたいのですが (たとえば、アップロード関数にさらにパラメーターを渡すなど)、それを行う方法が見つかりません...
どこが間違っているのか教えてください。可能であれば、フォームからファイルをアップロードする方法を教えてもらえますか?