通过ajax调用下载不在php中工作

时间:2018-03-27 12:08:04

标签: php jquery ajax

我使用下面的代码行

在html表中呈现文件详细信息
for(var counterOfCerticateLoop in certificateDetailsArray)
{
   var document_type_id = certificateDetailsArray[counterOfCerticateLoop]['document_type_id'];
   var document_actual_name = certificateDetailsArray[counterOfCerticateLoop]['document_actual_name'];
   var document_unique_name = certificateDetailsArray[counterOfCerticateLoop]['document_unique_name'];
   var document_type = certificateDetailsArray[counterOfCerticateLoop]['document_type'];
   var document_description = certificateDetailsArray[counterOfCerticateLoop]['document_description'];
   var fileURL = '../student/certificates/'+document_unique_name;
   var EditButton = '<button title="Edit" onclick="editContactDetail(\''+document_type_id+'\')" class="btn btn-primary table_btn btn-outline btn-sm"><i class="fa fa-pencil"></i></button>';
   var DeleteButton = '<button title="Delete" onclick="deleteContactDetail(\''+document_type_id+'\')" class="btn btn-danger  table_btn btn-outline btn-sm"><i class="fa fa-trash"></i></button>';
   var DownloadButton = '<button title="Delete" onclick="download(\''+fileURL+'\')" class="btn btn-danger  table_btn btn-outline btn-sm"><i class="fa fa-download"></i></button>';

   var tableRow = '<tr><td>'+document_type+'</td>'+
           '<td>' + document_actual_name + '</td>'+
           '<td>'+document_description+'</td>'+
           '<td>'+DownloadButton+EditButton+DeleteButton+'</td>';
           $("#certificateTable").append(tableRow);
           }

  function download(url) 
  {    
       $.ajax({
                    url: '../common/downloadFile.php?path='+url,                       
                    type: 'post',
                    success: function(php_script_response){    
                           window.location = php_script_response;
                    }
            });
  }

所有这些代码行都是写入的视图文件。所有信息都完美地在html表中呈现

在downloadFile.php中,代码就像

       <?php

$path = $_REQUEST['path'];
if (file_exists($path))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

3 个答案:

答案 0 :(得分:0)

我担心无法通过AJAX下载文件,您可以在此问题下找到解释: Download a file by jQuery.Ajax

但我不知道你为什么要使用AJAX。

您可以执行以下操作:

class SocketIOManager: NSObject {

    static let sharedInstance = SocketIOManager()

    let manager = SocketManager(socketURL: URL(string: "")!, config: [.log(false), .compress, .forcePolling(false)])

    var avaialableCallBack:(([Any]) -> Void)?

    override init(){
        super.init()
    }



    func establishConnection() {

        let socket = manager.socket(forNamespace: "/consumer")

        socket.on("connect") { (data, ack) -> Void in
            print("socket connected",data,ack)
        }

        socket.on(clientEvent: .disconnect){data, ack in
            print("socket disconnected")
        }

        socket.on("session-available") { (dataArr, ack) -> Void in
            ack.with(true)
            if let sessionAvailableCB = self.avaialableCallBack {
                sessionAvailableCB(dataArr)
            }
        }

        socket.connect()
    }

    func closeConnection() {
        let socket = manager.socket(forNamespace: "/consumer")
        socket.disconnect()
    }

    func emitMessage(message:String,data:[String:Any]){
        let socket = manager.socket(forNamespace: "/consumer")
        socket.emit(message,data)
    }

    func emitMessageWithAck(message:String,data:[String:Any]) -> OnAckCallback{
        let socket = manager.socket(forNamespace: "/consumer")
        return socket.emitWithAck(message, data)
    }



}

所以基本上,跳过javascript并直接将用户导航到将文件发送给用户的脚本,目标=&#34; _blank&#34;文件下载将从新标签开始,因此不会关闭或重定向当前窗口/标签。

答案 1 :(得分:0)

您需要创建一个包含属性target="_blank"的链接,该链接指向您的下载页面(例如:'http://example.com/common/downloadFile.php?path='+url

当打开新窗口并下载内容时,将关闭新窗口(默认情况下在现代浏览器中)

因此,在您的代码中,您可以修改download函数,如下所示:

function download(url) { 
    var link = $('<a>', {
        href: '../common/downloadFile.php?path='+url,
        target: '_blank'
    });
    $(link)[0].click();
}

答案 2 :(得分:0)

我通过以下代码

重新获得了它
  function download(document_unique_name,document_actual_name) 
  {   
      window.location =  '../common/downloadFile.php?document_unique_name='+document_unique_name+'&document_actual_name='+document_actual_name; 
  }

它对我有用!!!