如何在单页应用程序中设置文件下载,而不会触发重新加载?
我遇到过在服务器上生成PDF文件并需要提供给客户端进行下载的情况.将其作为application/octet-stream发送在SPA中没有任何用处,因为无法通过AJAX发送文件.
我想出的最好的方法是将生成的文件保存在服务器上的临时文件夹中,将文件的URL发送到客户端并进行操作window.open(url).问题是不同的浏览器以不同的方式打开文件.例如,Firefox默认情况下使用它们在同一个选项卡中打开PDF,从而PDF.js破坏整个SPA的想法.但做一个window.open(url, '_blank')经常触发弹出窗口拦截器等.其他文件类型可以导致上帝知道什么...
在SPA中下载文件是否有跨浏览器,安全,通用的方法?
我正在研究Hottowel项目,并更新到Durandal 2.0.我已经按照从1.x转换为2.0指南,但我仍然有一些问题:
1.加载router插件总是超时,除非我像这样加载它:
app.configurePlugins({
dialog: true,
router: true
}, '../durandal/plugins');
Run Code Online (Sandbox Code Playgroud)
请注意path参数.
2.我可以从一个模块到另一个不再过渡.模块可以加载的唯一方法是使用URL中的特定哈希刷新页面.当我单击导航按钮时,浏览器URL中的哈希值会更改,但不会加载模块.还有,router.isNavigating坚持下去true.
这是文件:
main.js
require.config({
paths: {
'text': '../Scripts/text',
'durandal': '../Scripts/durandal',
'plugins': '../Scripts/durandal/plugins',
'transitions': '../Scripts/durandal/transitions',
'services' : 'services'
}
});
define('jquery', function () { return jQuery; });
define('knockout', ko);
define(['durandal/app', 'durandal/viewLocator', 'durandal/system', 'services/logger'],
function (app, viewLocator, system, logger)
system.debug(true);
app.title = 'MyApp';
app.configurePlugins({
dialog: true,
router: true
}, '../durandal/plugins');
app.start().then(function () {
viewLocator.useConvention();
app.setRoot('viewmodels/shell', 'entrance');
});
});
Run Code Online (Sandbox Code Playgroud)
shell.js
define(['durandal/plugins/router', 'durandal/app', …Run Code Online (Sandbox Code Playgroud)