0
我最初试图实现一个用于上传文件的Javascript函数,但文件永远不会上传,我无法弄清楚如何使用该代码。在Play框架中上传图片
这是“查看文件”:
<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);
}
}
}
}
我想自定义(如更多参数传递到上传功能),但不能找到一种方法来做到这一点...
你能告诉我我做错了什么,如果可能你能指导我通过表单上传文件吗?
我觉得这是一个很好的问题,但我会建议您更改其演示文稿。 1)请添加您尝试过的链接,并举例说明失败的例子2)重新提出问题听起来要求不高。 –