2016-01-18 37 views
1

單擊CMS中的按鈕下載PDF文件時出現問題。我正在使用模塊BetterButtons。按鈕本身工作正常,但下載文件時出現錯誤。我總是得到這個錯誤:Silverstripe 3.2:如何使用按鈕強制從CMS下載文件?

Failed to load resource: the server responded with a status of 500 (Warning at line 257 of /Applications/MAMP/htdocs/myproject/framework/control/HTTPResponse.php)

[Recoverable Error] Object of class SS_HTTPResponse could not be converted to string.

所以我假定行

return SS_HTTPRequest::send_file($filedata, $fileName, 'application/pdf');

錯誤或東西是錯誤的$ FILEDATA ...什麼是正確的方式要做到這一點?

我的代碼:

在我的數據對象,我有下載按鈕:

private static $better_buttons_actions = array (
      'printFilesPDF' 
); 

public function getBetterButtonsActions() { 
     $fields = parent::getBetterButtonsActions(); 
     $fields->push(BetterButtonCustomAction::create('printFilesPDF', 'Print files')); 
     return $fields; 
} 

public function printFilesPDF() { 

     $filedata = File::find("assets/PDF/myFile.pdf"); 
     $fileName = "myFile.pdf"; 
     return SS_HTTPRequest::send_file($filedata, $fileName, 'application/pdf'); 

} 
+0

每當你得到一個「500錯誤」回到你需要改變開發模式,會給你一個錯誤,將可能幫助你解決或someelse在這裏幫助你解決 – Barry

+0

嗨。謝謝你回答這麼快。我處於開發模式..在單擊按鈕後重新加載頁面後發現「真實」錯誤:[可恢復錯誤]類SS_HTTPResponse的對象無法轉換爲字符串。不幸的是,仍然不知道該怎麼辦:(你能幫我嗎? – iraira

+0

@iraira我遇到了同樣的問題。你找到了解決方案嗎? –

回答

2

我找到了解決辦法。這不是最好的,但它適用於我。

我讓文件在新標籤中打開,以便用戶可以自行下載或只是看它在瀏覽器窗口:

首先而是採用了BetterButtonCustomAction我用BetterButtonLink的:

public function getBetterButtonsActions() { 
    $fields = parent::getBetterButtonsActions(); 
    $fields->push(new BetterButtonLink('Download the file', "assets/myfile.pdf")); 
} 

通過使用BetterButtonLink我遇到了以下問題: 每次點擊該按鈕時,它都會嘗試通過AJAX請求在CMS管理員中打開PDF,從而導致顯示編碼文本。一個簡單的解決方案,你可以在這裏找到:https://github.com/unclecheese/silverstripe-gridfield-betterbuttons/issues/64

所以我最後的代碼是

public function getBetterButtonsActions() { 
    $fields = parent::getBetterButtonsActions(); 
    $fields->push(new BetterButtonLink_NewWindow('Download the file', "assets/myfile.pdf")); 
} 

而且BetterButtonLink_NewWindow代碼:

class BetterButtonLink_NewWindow extends BetterButtonLink { 

    /** 
    * Gets the HTML representing the button 
    * @return string 
    */ 
    public function getButtonHTML() { 
     return sprintf(
      '<a class="ss-ui-button %s" href="%s" target="_blank">%s</a>', 
      $this->extraClass(), 
      $this->getButtonLink(), 
      $this->getButtonText() 
     ); 
    } 

} 
+0

非常感謝分享! –

1

傳遞一個「文件」而不是反對你打算將字符串傳遞到由send_file功能。

我會改變線路

$filedata = File::find("assets/PDF/myFile.pdf"); 

$filedata = file_get_contents(File::find("assets/PDF/myFile.pdf")->getFullPath()); 

然而,這將更好的寫法,以檢查文件::查找(...)返回一個文件,而不是空值!

+0

我試過你的解決方案,但仍然得到相同的錯誤:「SS_HTTPResponse類的對象無法轉換爲字符串」 – iraira

+0

你可以測試一個簡單的返回像'返回SS_HTTPRequest :: send_file(「test」,「test.html」,'text/html');'第一個。只是爲了確定錯誤在哪裏。巴里斯小費應該可以工作,但你仍然得到那個似乎很奇怪的錯誤。什麼是你使用的SS版本? –

+0

所以我的意見是糾正發送到HTTP請求的數據。我也不確定你是如何使用這個請求的,因爲你似乎正在將它添加到一個動作中......通常我會在控制器上做出最後一個動作函數的返回......你可以試試這個嗎?一個簡單的控制器,輸出這個作爲它的一個行動沒有更好的按鈕的東西。 – Barry

1

File :: find()在文件存在時返回一個File對象,如果不存在,則返回null。它從不返回二進制數據,這就是HTTPResponse不會將其轉換爲字符串的原因。

如果file_get_contents不起作用,請檢查它是否在您的機器上禁用。

//attempt to find the file 
$file = File::find("path/to/my/file.txt"); 

//you need to check that the file is File and not null 
if($file && $file instanceof File) { 
    //get the file path 
    $path = $file->getFullPath(); 

    //get the file data. If file_get_contents() doesn't work, you might need fopen() or file() instead 
    $fileData = file_get_contents($path); 
    $fileName = "myFile.pdf"; 
    return SS_HTTPRequest::send_file($fileData, $fileName, 'application/pdf'); 
} 
+0

Ok ..我做了一些測試,發現你的代碼在前端完美工作(例如,當我點擊模板中的按鈕時),但是當我觸發這個功能時我仍然得到相同的錯誤從CMS ...你知道原因可能是什麼嗎? – iraira

0

我們可以用一個LiteralField創建一個鏈接到文件中。通過將鏈接的類設置爲ss-ui-button,我們可以使鏈接看起來像一個按鈕。

這裏是一個例子鏈接到一個特定的文件:

LiteralField::create(
    'DownloadLink', 
    '<a href="assets/myfile.pdf" target="_blank" class="ss-ui-button" download>Download the file</a>' 
); 

這可能在getCMSFieldsgetCMSActions被用來製造在田地區域或按鈕區域中的按鈕顯示。

這裏是通過getCMSFields一個例子鏈接到一個$has_one控制下的文件:

public function getCMSFields() { 
    $fields = parent::getCMSFields(); 
    if ($this->File()->exists()) { 
     $fields->addFieldToTab('Root.Main', 
      LiteralField::create(
       'DownloadLink', 
       '<a href="' . $this->File()->Link() . '" target="_blank" class="ss-ui-button" download>Download the file</a>' 
      ) 
     ); 
    } 
    return $fields; 
}