我最初尝试实现用于上传文件的 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);
}
}
}
}
我想自定义它(例如将更多参数传递给上传函数),但找不到这样做的方法......
你能告诉我哪里做错了吗,如果可能的话,你能指导我通过表格上传文件吗?