PlayFramework:Ajax + Drag n' Drop + File Upload +控制器中的文件

PlayFramework: Ajax + Drag n#39; Drop + File Upload + File object in controller?(PlayFramework:Ajax + Drag n Drop + File Upload +控制器中的文件对象?)
本文介绍了PlayFramework:Ajax + Drag n' Drop + File Upload +控制器中的文件对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

有谁知道通过 Ajax 上传文件并使用从桌面拖放支持 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

我们的控制器是Application,我将使用它来保持简单.

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

在 app/views/Application 文件夹/包中编辑您的 Application.html

#{extends 'main.html'
                
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Drag amp; Drop with Protractor by Repeater(拖动amp;通过中继器使用量角器掉落)
Detecting HTML5 Drag And Drop support in javascript(在 javascript 中检测 HTML5 拖放支持)
HTML5: dragover(), drop(): how get current x,y coordinates?(HTML5: dragover(), drop(): 如何获取当前 x,y 坐标?)
How to make lt;divgt; resizeable?(如何制作lt;divgt;可调整大小?)
Internet Explorer 9 Drag and Drop (DnD)(Internet Explorer 9 拖放 (DnD))
D3 Differentiate between click and drag for an element which has a drag behavior(D3区分具有拖动行为的元素的单击和拖动)