播放框架:控制器中的Ajax Drag n’Drop File Upload File对象

栏目: jQuery · 发布时间: 5年前

内容简介:我尝试了几种不同的方法,没有任何正常的工作.代码日志版权声明:翻译自:http://stackoverflow.com/questions/4867007/playframework-ajax-drag-n-drop-file-upload-file-object-in-controller

我尝试了几种不同的方法,没有任何正常的工作.

这是我的成功尝试:

编辑路径文件并添加

POST    /upload                                 Application.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>

最后的笔记

记得从 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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Thinking Recursively

Thinking Recursively

Eric S. Roberts / Wiley / 1986-1-17 / USD 85.67

The process of solving large problems by breaking them down into smaller, more simple problems that have identical forms. Thinking Recursively: A small text to solve large problems. Concentrating on t......一起来看看 《Thinking Recursively》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具