普通网友 2016-08-16 09:54
浏览 102
已采纳

上传文件后返回的下载文件:一个AJAX文件转换器

I'm programming a PHP function that encrypts a file using a specific key. I want the user to upload his file using the "browse..." window then get the "save as" window as a result after the php encrypting.

I'm working in full AJAX so my upload method uses a hidden form and a hidden iframe to handle the upload request.

Javascript Part :

<script>
    $("#encryptFileButton").button().click(clickEncryptFile);


    function clickEncryptFile() {
        if (!jQuery.browser.msie) {
            $("#encryptFileBrowse").click();
        } else {... not the matter of the question ...}
    }

    function encryptFileBrowseChange() {
        $("#uploadAttachmentForm").submit();
    }

</script>

The nice visible button

<button id="encryptFileButton" type="button" class="ui-..." role="button">Encrypt Now</button>

The hidden part

<form action="https://web.com/encryptPHP" id="uploadAttachmentForm" target="uploadTarget" style="display:none" enctype="multipart/form-data" method="post" accept-charset="utf-8">
    <input type="file" name="encryptFileBrowse" id="encryptFileBrowse" onchange="encryptFileBrowseChange()">
</form>

<iframe id="uploadTarget" name="uploadTarget" src=""></iframe>

Now on the server side, the encryptPHP does the job and returns the encrypted file with all the required HTTP headers. But the browser does not open the "save as window"

Here is the Chrome inspector of the request

enter image description here

Can you help me to fill the download part of this problem ?

I wouldn't like using a temporary file on the server to be stateless and prevent orphan files when browser is interrupted. I'd really like to make this with only one query.

  • 写回答

1条回答 默认 最新

  • duanji9378 2016-08-16 12:19
    关注

    In the server response, Content-disposition is inline, it must be content-disposition: attachment to get the "save as " popup.

    I don't delete my question as the code example is pretty and usefull.

    Uploading and downloading in only one request is nice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘