【问题标题】:shows me blank page when upload a .docx document on a website laravel在网站 laravel 上上传 .docx 文档时显示空白页
【发布时间】:2017-02-01 01:01:20
【问题描述】:

你好,我是 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 工作表不同。请帮帮我。提前谢谢你

【问题讨论】:

  • 这是我点击文件的观点……这里可能做错了什么,但我不这么认为,但以防万一……<td><a href="/open-uploaded-file"> {{ $upload['file_name'] }} </a></td>
  • if (File::isFile($file_name){ } 之后添加一个else{ ... } 块,您将在其中返回有关找不到文件的错误响应。看看这是否返回错误(意味着文件不存在)。
  • 是的,我刚刚添加了else { Session::flash("message", "File Not found"); return redirect("\home"); },它说“找不到文件”
  • 所以你有你的罪魁祸首。您需要确保知道文件的存储位置。
  • 我知道文件的存储位置,但我没有办法给它提供该路径以便它可以提取文件。如果您在代码中看到第三行,那是我在尝试了所有可能的可能性后注释掉的文件的路径。我尝试取消注释并执行此操作,但仍然找不到相同的空白页或路径

标签: laravel-5 uploading


【解决方案1】:

这么久终于想通了。

它发送“null”作为文件名,所以我现在所做的是从数据库而不是存储中获取文件名,然后比较它是否该文件在存储和数据库中然后打开它。下面是现在为我运行的代码。 感谢@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 的所有帮助。你给了我修复它的动力加上你在if 条件中缺少的else{...} 部分实际上帮助我找到了错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2016-05-11
    • 2012-01-07
    • 2015-08-10
    • 2013-08-27
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多