2016-09-23 73 views
1

你好,我是新來laravel和仍然在學習它,我遇到不同的困難和問題,我不能找到一種方法來解決它。我很久以來一直堅持這一點。任何幫助將非常感激。顯示我的空白頁上載的.docx文檔時,在網站上laravel

的問題

每當我上傳.docx文件到我的網站,我得到了一個空白頁面沒有任何錯誤或不具有的docx文件的內容。我不知道問題是什麼。這個你能幫我嗎。

代碼

public function getUploadedFile() { 

    // $destinationPath = 'uploads'; 
    // $path= public_path()."/". $destinationPath; 

    // $content = utf8_encode(File::get('/var/www/html/asad/File-Uploader/public/uploads/detailed_period_report_template.xls')); 
    // return view('/files', compact('path')); 


    $file = $this->request->file('file_name'); 
    $file_name = $this->request->file_name; 

    if (File::isFile($file_name)) 
    { 
     $file_name = File::get($file_name); 
     $response = Response::make($file_name, 200); 
     $content_types = [ 
      'application/octet-stream', // txt etc 
      'application/msword', // doc 
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document', //docx 
      'application/vnd.ms-excel', // xls 
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // xlsx 
      'application/pdf', // pdf 
     ]; 
     // using this will allow you to do some checks on it (if pdf/docx/doc/xls/xlsx) 
     $response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 

     return $response; 
    } 

我沒有這方面的任何觀點。我需要一個嗎?如果是的話,會是什麼樣子,因爲每個docx文件都有一組屬性,如字體,大小等,而不像excel表格,我們只是定義了列。請幫助我。謝謝你在前進

+0

這是我的看法對文件單擊雖然...也許在這裏做一些錯誤的,但我不這麼認爲,但以防萬一... '​​ {{ $upload['file_name'] }}' –

+0

添加'其他{... }'if(File :: isFile($ file_name){}''後面的塊會返回一個關於找不到文件的錯誤響應,看看是否返回錯誤(表示文件不存在) – Matey

+0

我剛剛添加了'else { Session :: flash(「message」,「File Not found」); return redirect(「\ home」); }' 並且它顯示「文件未找到」 –

回答

0

我已經想通了後這麼久終於。

有人發出「空」爲使我做了什麼,現在是從數據庫中,而不是存儲得到了文件名,然後進行了比較,如果該文件是存儲和數據庫,然後打開它的文件名。下面是現在爲我運行的代碼。 謝謝@MATEY所有的幫助,雖然

 $upload = new Upload; 

     $file= public_path(). "/uploads/" . $upload->file_name = $this->request->file_name; 
     //dd($file); 

     $files = File::get($file); 
     $response = Response::make($files, 200); 

,並改變了內容類型$response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

謝謝你的所有幫助@MATEY。你給我動力修復它,加上你的else{...}部分缺失if條件實際上幫助我很多發現錯誤。