2009-12-31 61 views
2

我正在爲我的辦公室編寫文件共享應用程序。我正在經歷的一個奇怪問題是,當您點擊下載按鈕時,將以PDF格式打開Illustrator文件。Illustrator以PDF格式打開

由於MIME類型的插圖文件是application/pdf,因此會觸發此問題。所以瀏覽器在讀取文件時觸發Acrobat打開文件。有什麼辦法可以指導瀏覽器在Illustrator中打開文件嗎?

或者有什麼方法可以在上傳文件後修改MIME類型?後端代碼是PHP。

謝謝你的幫助。

回答

3

一種方法是強制瀏覽器顯示「下載文件」-dialog。因此用戶可以決定如何處理文件。

這可以通過PHP頭來完成。 (http://www.php.net/manual/en/function.header.php#83384

也有關於如何這個(帖子83384)爲例:

<?php 
    // downloading a file 
    $filename = $_GET['path']; 

    // fix for IE catching or PHP bug issue 
    header("Pragma: public"); 
    header("Expires: 0"); // set expiration time 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    // browser must download file from server instead of cache 

    // force download dialog 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 

    // use the Content-Disposition header to supply a recommended filename and 
    // force the browser to display the save dialog. 
    header("Content-Disposition: attachment; filename=".basename($filename).";"); 

    /* 
    The Content-transfer-encoding header should be binary, since the file will be read 
    directly from the disk and the raw bytes passed to the downloading computer. 
    The Content-length header is useful to set for downloads. The browser will be able  to 
    show a progress meter as a file downloads. The content-lenght can be determines by 
    filesize function returns the size of a file. 
    */ 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".filesize($filename)); 

    @readfile($filename); 
    exit(0); 
?> 

使用此示例時,請考慮使用

$filename = $_GET['path']; 

是一個很大的安全問題。你應該使用類似ID的東西來代替或者驗證輸入。 例如:

if($_GET['file'] == 1) { 
    $filename = foobar.pdf; 
} elseif($_GET['file'] == 2) { 
    $filename = foo.pdf; 
} else { 
    die(); 
} 
+0

謝謝。我很被拿走,我沒有想到做一個'readfile'調用。 – Nirmal 2010-01-04 04:57:46

+0

是的,除非Illustrator文件以Illustrator格式* .ai *保存,否則這是最佳解決方案。 – 2015-05-18 12:29:08