从WKWebView上的服务器读取docx文件

时间:2018-03-15 17:41:45

标签: php swift docx

如何在具有Web视图的视图上的PHP服务器上显示生成的.docx文件?

如果我将网址更改为https://www.google.ca/,则会在视图中显示,但当我尝试阅读http://blog.local:4711/api/invoices/69/download时,会显示空白区域。

在使用断点的控制台上,我看到它转到了正确的行。

在任何浏览器上使用相同的网址(http://blog.local:4711/api/invoices/69/download),我都可以下载该文件。

swift code:

import UIKit
import WebKit

class InvoicePreviewViewController: UIViewController {

    @IBOutlet weak var preview: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let url = URL(string: "http://blog.local:4711/api/invoices/69/download")

        if let unwrappedURL = url {
            let request = URLRequest(url: unwrappedURL)
            let session = URLSession.shared

            let task = session.dataTask(with: request) { (data, response, error) in
                if error == nil {
                    DispatchQueue.main.async {

                        self.preview.load(request)
                    }
                } else {
                    print("no")
                }
            }
            task.resume()
        }
    }

}

PHP代码

 ...
        $filename = $this->filename;        
        $this->_templateProcessor->saveAs($filename);
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filename));
        flush();
        readfile($filename);
        unlink($filename); // deletes the temporary file
        exit;

1 个答案:

答案 0 :(得分:0)

遵循约翰·埃尔莫尔的回答它起作用.....但是,我不知道为什么......只有在我重新启动Xcode之后。