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)重新提出問題聽起來要求不高。 –