播放框架:控制器中的Ajax Drag n’Drop File Upload File物件
我嘗試了幾種不同的方法,沒有任何正常的工作.
這是我的成功嘗試:
編輯路徑檔案並新增
POST/uploadApplication.upload
我們的控制器是應用程式,我將使用它來保持簡單.
編輯您的應用程式控制器類
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' /} #{set title:'Multiple Uploads' /} <div id="file-uploader"> <noscript> <p>Please enable JavaScript to use file uploader.</p> <!-- or put a simple form for upload here --> </noscript> <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>
編輯您的主要佈局:main.html,位於app / views資料夾/包中,並在jQuery之後新增此行
<script src="@{'/public/javascripts/client/fileuploader.js'}" type="text/javascript"></script>
最後的筆記
記得從ofollow,noindex" target="_blank">AJAX Upload Valums 下載指令碼,享受!
你也可以grab the source here .
我在不同的瀏覽器中測試了它,至少對我來說是適用的.在Riyad玩的信用!郵件列表誰暗示了我有關請求
P.S:我正在使用我之前發表的評論
編輯
程式碼的答案已經按照T.J.的指示新增. Crowder,我同意:)
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/4867007/playframework-ajax-drag-n-drop-file-upload-file-object-in-controller