我正在创建一个文件服务器,用户可以选择加密他们的文件,理想情况下,在浏览器中输入密码即可动态解密并下载它们(当然,以分块方式,对PHP更容易)。解密的数据永远不应保存到磁盘。
所以现在用户输入密码,jQuery发送一个帖子到下载页面,密码和变量“checking”设置为true。然后下载页面返回一个带有true或false的json对象,指示它是否可以解密文件,这部分工作正常。
根据响应,如果响应变量为false,它将要求用户输入另一个密码,或者如果原始响应为真,它将仅使用密码发送新请求并检查设置为false(正确)密码)。
PHP然后响应要下载的文件的第一个块。问题是,Firefox永远不会出现下载提示。显然你不能尝试使用jquery $ .post()
下载文件所以,我想我需要另一种方法来做这个客户端部分。任何帮助将不胜感激:)
这是适用的客户端代码。
Download: function(item) {
if (item) { var file = Content.files[(item ? $(item).attr('id').split('_')[1] : Content.selected.files[0])]; }
else { if (Content.selected.files.length != 1 || Content.selected.folders.length != 0) { return; } var item = Content['files'][Content.selected.files[0]]; }
if (file['encrypted']) {
$.prompt({
input: {
html: "Please enter the password to download this encrypted file: <br /><br /><input type='password' name='password' style='width:400px;' />",
buttons: { "Submit" : true },
persistent: false,
submit: function(event, value, message, form) {
$.prompt.goToState('pause');
$.post("?p=download&id="+file['id'], {
password: form.password, checking: true
}, function (response) {
if (response.ok) {
$.post("?p=download&id="+file['id'],{ password: form.password, checking: false }); $.prompt.close();
} else {
$.prompt.getStateContent('output').find('.jqimessage').html(response.result);
$.prompt.goToState('output');
}
},"JSON");
return false;
}
},
pause: {
html: 'Please Wait... <img src="images/load_icon.gif" alt="" style="margin-bottom:-2px;margin-left:4px;height:12px;" />',
buttons: { },
persistent: true,
},
output: {
buttons: { Close: "Close" },
persistent: false
}
});
} else {
window.location.href = "?p=download&id="+file['id'];
}
},
答案 0 :(得分:0)
我看到你正在使用jQuery,所以你可以尝试https://github.com/johnculviner/jquery.fileDownload,它也适用于POST。
答案 1 :(得分:0)
我想使用临时的传统形式是一个很好的解决方法。
Download: function(item) {
if (item) { var file = Content.files[(item ? $(item).attr('id').split('_')[1] : Content.selected.files[0])]; }
else { if (Content.selected.files.length != 1 || Content.selected.folders.length != 0) { return; } var item = Content['files'][Content.selected.files[0]]; }
if (file['encrypted']) {
$.prompt({
input: {
html: "Please enter the password to download this encrypted file: <br /><br /><input type='password' name='password' style='width:400px;' />",
buttons: { "Submit" : true },
persistent: false,
submit: function(event, value, message, form) {
$.prompt.goToState('pause');
$.post("?p=download&id="+file['id'], {
password: form.password, checking: true
}, function (response) {
if (response.ok) {
$('body').append('<form id="encrypted_download" method="post" action="?p=download&id='+file['id']+'" style="display:none;"><input type="hidden" name="password" value="'+form.password+'" /><input type="hidden" name="checking" value="false" /></form>');
$('#encrypted_download').submit().remove(); $.prompt.close();
} else {
$.prompt.getStateContent('output').find('.jqimessage').html(response.result);
$.prompt.goToState('output');
}
},"JSON");
return false;
}
},
pause: {
html: 'Please Wait... <img src="images/load_icon.gif" alt="" style="margin-bottom:-2px;margin-left:4px;height:12px;" />',
buttons: { },
persistent: true,
},
output: {
buttons: { Close: "Close" },
persistent: false
}
});
} else {
window.location.href = "?p=download&id="+file['id'];
}
},