有谁知道一种通过Ajax上载文件并使用支持PlayFramework将文件上载转换为File对象的功能的桌面拖放功能的方法吗?
我尝试了几种不同的方法,但均无法正常工作。
这是我成功的尝试:
编辑路线文件并添加
POST /upload Application.upload
我们的控制器是Application,我将使用它来简化它。
Application
编辑您的应用程序控制器类
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>
最后的注意事项 请记住从AJAX Upload Valums下载脚本,请尽情享受!
您也可以在这里获取源代码。
我至少在不同的浏览器中对其进行过测试。感谢Play的Riyad!邮件列表向我暗示了request.body
request.body
PS:我使用的是我之前发表过的评论
编辑 根据TJ Crowder的指示添加了带有代码的答案,我同意:)