本文介紹了PlayFramework:Ajax + Drag n' Drop + File Upload +控制器中的文件對象?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
有誰知道通過 Ajax 上傳文件并使用從桌面拖放支持 PlayFramework 將文件上傳轉(zhuǎn)換為 File 對象的能力的方法?
Does anyone know of a way to upload a file via Ajax and using drag n' drop from the desktop that supports PlayFramework's ability to convert file uploads to a File object?
我嘗試了幾種不同的方法,但都沒有正常工作.
I've tried several different methods, and nothing works correctly.
推薦答案
這是我的成功嘗試:
編輯路由文件并添加
POST /upload Application.upload
我們的控制器是Application
,我將使用它來保持簡單.
Our controller is Application
, I'll be using it to keep it simple.
編輯您的應(yīng)用程序控制器類
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 {
InputStream data = request.body;
moveTo = new FileOutputStream(new File(Play.getFile("").getAbsolutePath()) + File.separator + "uploads" + File.separator + filename);
IOUtils.copy(data, moveTo);
} catch (Exception ex) {
// catch file exception
// catch IO Exception later on
renderJSON("{success: false}");
}
}
renderJSON("{success: true}");
}
在 app/views/Application 文件夾/包中編輯您的 Application.html
#{extends 'main.html'
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!