有谁知道通过 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'
在 XHR 中使用 multipart/form-data 作为 Content-Type 时收Getting #39;400 Bad Request#39; when using multipart/form-data as Content-Type in XHR(在 XHR 中使用 multipart/form-data 作为 Content-Type 时收到
XHR.getAllResponseHeaders() 在 Chrome 60 中未按预期返回XHR.getAllResponseHeaders() does not return headers as expected in Chrome 60(XHR.getAllResponseHeaders() 在 Chrome 60 中未按预期返回标头)
如何在 Jquery Ajax 中向请求添加标头?How to add header to request in Jquery Ajax?(如何在 Jquery Ajax 中向请求添加标头?)
是否可以使用 XHR(“Ajax"请求)发送自定义标头Is it possible to send custom headers with an XHR (quot;Ajaxquot; request)?(是否可以使用 XHR(“Ajax请求)发送自定义标头?)
我可以为所有 AJAX 请求设置全局标头吗?Can I set a global header for all AJAX requests?(我可以为所有 AJAX 请求设置全局标头吗?)
如何从跨域 Ajax 请求访问 Content-Length 标头?How can I access the Content-Length header from a cross domain Ajax request?(如何从跨域 Ajax 请求访问 Content-Length 标头?)