PlayFramework:在控制器的Ajax +拖N'滴+文件上传+ File对象? [英] PlayFramework: Ajax + Drag n' Drop + File Upload + File object in controller?

查看:331
本文介绍了PlayFramework:在控制器的Ajax +拖N'滴+文件上传+ File对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道的方式上传通过Ajax的文件,并使用拖放N'一滴从支持PlayFramework的来上传文件转换为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

我们的控制器是应用程序,我会用它来保持它的简单。

Our controller is Application, I'll be using it to keep it simple.

编辑应用程序控制器类

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}");
} 

修改你的Application.html在app / views /应用程序文件夹/包

#{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上传Valums 下载脚本,享受!

Final notes Remember to download the script from AJAX Upload Valums, enjoy!

您也可以这里抓源头。

我在里面,至少对我的作品不同的浏览器进行了测试。积分到利雅得的游戏!邮件列表谁暗示我关于 request.body

I tested it in different browsers it works for me at least. Credits to Riyad in Play! mailing list who hinted me about the request.body

PS:我使用的是之前

P.S: I'm using the one I posted as a comment before

修改 与code答案已经被添加的指示,TJ克劳德,我同意:)

Edit The answer with code has been added as directed by T.J. Crowder, I agree :)

这篇关于PlayFramework:在控制器的Ajax +拖N'滴+文件上传+ File对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆