Php 使用jquery将数据发布到服务器并将响应下载为文件
我正在创建一个文件服务器,用户可以在其中选择加密他们的文件,理想情况下在浏览器中输入密码,以便动态解密并下载(当然,以分块方式,对PHP来说更容易)。解密后的数据不应保存到磁盘 因此,现在用户输入密码,jQuery向下载页面发送一篇文章,其中密码和变量“checking”设置为true。下载页面然后返回一个json对象,该对象带有true或false,指示它是否可以解密文件,这部分工作正常 根据响应的不同,如果响应变量为false,它将向用户请求另一个密码;如果原始响应为true(正确的密码),它将发送一个新的请求,其中仅包含密码并将检查设置为false PHP然后用要下载的第一个文件块进行响应。问题是,Firefox从来不会出现下载提示。显然,您不能尝试使用jquery$.post()下载文件 所以,我想我需要另一种方法来完成这个客户端部分。任何帮助都将不胜感激:) 以下是适用的客户端代码Php 使用jquery将数据发布到服务器并将响应下载为文件,php,jquery,http,post,download,Php,Jquery,Http,Post,Download,我正在创建一个文件服务器,用户可以在其中选择加密他们的文件,理想情况下在浏览器中输入密码,以便动态解密并下载(当然,以分块方式,对PHP来说更容易)。解密后的数据不应保存到磁盘 因此,现在用户输入密码,jQuery向下载页面发送一篇文章,其中密码和变量“checking”设置为true。下载页面然后返回一个json对象,该对象带有true或false,指示它是否可以解密文件,这部分工作正常 根据响应的不同,如果响应变量为false,它将向用户请求另一个密码;如果原始响应为true(正确的密码),
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'];
}
},
下载:功能(项){
if(item){var file=Content.files[(item?$(item.attr('id')).split('uU')[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(文件['encrypted']){
美元提示({
输入:{
html:“请输入密码以下载此加密文件:
”,
按钮:{“提交”:true},
坚持:错,
提交:功能(事件、值、消息、表单){
$.prompt.goToState('pause');
$.post(“?p=download&id=“+file['id']){
密码:form.password,检查:true
},功能(回应){
if(response.ok){
$.post(“?p=download&id=“+file['id'],{password:form.password,checking:false});$.prompt.close();
}否则{
$.prompt.getStateContent('output').find('.jqimessage').html(response.result);
$.prompt.goToState('output');
}
}“JSON”);
返回false;
}
},
暂停:{
html:“请稍候…”,
按钮:{},
是的,
},
输出:{
按钮:{关闭:“关闭”},
持续性:假
}
});
}否则{
window.location.href=“?p=download&id=“+文件['id'];
}
},
我知道您正在使用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) {
$('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'];
}
},
下载:功能(项){
if(item){var file=Content.files[(item?$(item.attr('id')).split('uU')[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(文件['encrypted']){
美元提示({
输入:{
html:“请输入密码以下载此加密文件:
”,
按钮:{“提交”:true},
坚持:错,
提交:功能(事件、值、消息、表单){
$.prompt.goToState('pause');
$.post(“?p=download&id=“+file['id']){
密码:form.password,检查:true
},功能(回应){
if(response.ok){
$('body')。追加('');
$(“#加密的_下载”).submit().remove();$.prompt.close();
}否则{
$.prompt.getStateContent('output').find('.jqimessage').html(response.result);
$.prompt.goToState('output');
}
}“JSON”);
返回false;
}
},
暂停:{
html:“请稍候…”,
按钮:{},
是的,
},
输出:{
按钮:{关闭:“关闭”},
持续性:假
}
});
}否则{
window.location.href=“?p=download&id=“+文件['id'];
}
},
ajax不适用于下载…使用window.location
重定向到下载文件,如果服务器设置为强制下载,则不会更改页面,但它需要发布文件的密码,这就是我无法使用window.location.href的原因。我想我可以将密码放在URL中,但这似乎是个糟糕的主意。只需使用表单让用户输入密码,然后在没有ajax的情况下将其发布到输出文件的URL中,在上面的帖子中我该如何做呢?它需要在javascript中完成,而不是在传统的html事件中。使用具有相同样式的表单,而不是prompt