简体   繁体   English

下载文件而不是打开新窗口

[英]Download the file instead of opening new window

Users can upload documents and details on my form which gets saved into the database and a folder is created to hold the documents. 用户可以在我的表单上上传文档和详细信息,并将其保存到数据库中,并创建一个文件夹来保存文档。 They can upload .jpg .png .docx etc.. so not just images. 他们可以上传.jpg .png .docx等。

I now want to display links to these documents and when you click on the link it gives the option to save/open the file on your computer. 我现在想显示这些文档的链接,当您单击链接时,它提供了在计算机上保存/打开文件的选项。 Instead when I click the link it goes to a new page and shows the image there. 相反,当我单击链接时,它会转到一个新页面并在其中显示图像。 but because they can upload word documents pdf etc.. you cant see these ina new window. 但由于他们可以上传pdf等Word文档。因此您无法在新窗口中看到这些文件。

The method im using now doesnt support on IE which is essential. 我现在使用的方法不支持IE,这是必不可少的。

Code: 码:

  if(isset($_POST["submit"])){

        $ref = $_POST["reference"];
        $query  = "SELECT * ";
        $query .= "FROM customers ";
        $query .= "WHERE reference = {$ref}";
        $results = mysqli_query($connection, $query);

        // Array to hold all results:
        $all = array();
        while ($customer_details = mysqli_fetch_assoc($results)) {
            // Append a new sub-array of the 3 other cols to
            // the main array's key by reference using the []
            // array append syntax
            $all[$customer_details['reference']][] = array(
                'doc1' => $customer_details['doc1'],
                'doc2' => $customer_details['doc2'],
                'doc3' => $customer_details['doc3']
            );
        }

        foreach ($all as $reference => $rows) {
          // Write out the reference, which was the array key
            echo $reference . "<br/>";
            // Then in a loop, write out the others
            foreach ($all as $reference => $rows) {
          // Write out the reference, which was the array key
            echo $reference . "<br/>";
            // Then in a loop, write out the others
            foreach ($rows as $row) {
                echo  "<a href='docs/1111111/1/" . $row['doc1'] . "' download='" . $row['doc1'] . "' >file1</a><br/>";
                echo  "<a href='docs/1111111/1/" . $row['doc2'] . "' download='" . $row['doc2'] . "' >file2</a><br/>";
                echo  "<a href='docs/1111111/1/" . $row['doc3'] . "' download='" . $row['doc3'] . "' >file3</a><br/>";
            }

        }
    }

How can i get around this? 我该如何解决?

You only need to add code lines into your .htaccess file on server. 您只需要在服务器上的.htaccess文件中添加代码行即可。 You can change file type depending on extension (ex. doc) 您可以根据扩展名(例如doc)更改文件类型

<FilesMatch "\.(?i:pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

Here you got 3 options, from which this one is 3rd: http://www.tipsandtricks-hq.com/forum/topic/force-a-file-to-download-instead-of-showing-up-in-the-browser 在这里,您有3个选项,其中第3个选项: http : //www.tipsandtricks-hq.com/forum/topic/force-a-file-to-download-instead-of-showing-up-in-the -browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM