2

我一直在尝试从这样的特定目录下载我上传的文件:

Yii::app()->request->SendFile($model->statement, 
                        file_get_contents($path.$model->statement)
                        ); 

它识别文件并以正确的名称和格式下载它,但我得到的不是一个 74KB 的文件,而是一个 1KB 的损坏文件。试过其他文件如.doc、.pdf,还是一样。

我不完全确定 file_get_contents 实际上做了什么,因为我从我找到的一个例子中得到了这个。

要下载的文件以前是使用上传的

<div class="row">
    <?php echo $form->labelEx($model, 'statement'); ?>
    <?php echo $form->fileField($model, 'statement', array('size' => 36, 'maxlength' => 255)); ?>
    <?php echo $form->error($model, 'statement'); ?>
</div>

在 _form.php 并调整控制器中的 actionCreate:

public function actionCreate($id)
{
    $model=new Witness;
    $model->ip_id = $id;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    $path = Yii::app()->basePath . '/modules/iris/statements';
    if (!is_dir($path)) {
        mkdir($path);
    }   

    if(isset($_POST['Witness']))
    {
        $model->attributes=$_POST['Witness'];

        if (@!empty($_FILES['Witness']['name']['statement'])) {
            $model->statement = $_POST['Witness']['statement'];
            if ($model->validate(array('statement'))) {
                $model->statement = CUploadedFile::getInstance($model, 'statement');
            } else {
                $model->statement = '';
            }

            $model->statement->saveAs($path . '/' . time() . '_' . str_replace(' ', '_', strtolower($model->statement)));

            //$model->doc_type = $model->statement->getType();
            //$model->doc_size = $model->statement->getSize();
        }

        $model->statement = time() . '_' . str_replace(' ', '_', strtolower($model->statement));


        if($model->save())
            $this->redirect(array('ip/view','id'=>$model->ip_id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

上传的文件可以毫无问题地打开。知道为什么下载损坏了吗?下载前是否必须先指定文件的类型/大小?

4

4 回答 4

0

在 Yii2 中,一个sendFile()签名是

public $this sendFile ( $filePath, $attachmentName = null, $options = [] )

发送文件:

Yii::$app->response->sendFile('test.zip');

对于那些获得完整文件但仍然损坏的人:

您应该检查其他 php 文件是否正在打印输出,有时关闭 PHP 标记后的空格会导致问题。

从这里

于 2020-04-22T15:14:22.993 回答
0

我找到了另一种解决方案,而不是使用 SendFile 函数。将其放入视图文件以生成下载链接:

CHtml::link('Click to download file', array('ip/download&id='.$model->file_id));

其中“ip”是型号名称。这会将 file_id 传递给在 controller.php 中创建的函数 actionDownload:

public function downloadFile($fullpath){
    if(!empty($fullpath)){
        header("Content-type:application/pdf"); //for pdf file
        //header('Content-Type:text/plain; charset=ISO-8859-15');
        //if you want to read text file using text/plain header
        header('Content-Disposition: attachment; filename="'.basename($fullpath).'"');
        header('Content-Length: ' . filesize($fullpath));
        readfile($fullpath);
        Yii::app()->end();
    }
}

public function actionDownload($id){
    $model=Ip::model()->findByPk($id);
    $path = Yii::app()->basePath . 'location of file to be downloaded'.$model->filename;
    $this->downloadFile($path);
}

这应该完全下载文件。不用担心内容类型,我已经尝试了所有常见的文件类型(.pdf、.doc、.ppt、.png、.jpg),一切都很好。

于 2015-11-25T07:47:26.423 回答
0

如果查看 的参数列表sendFile,您可以看到第三个参数是mimeType。事实上,您没有提供 HTTP 标头响应,因此我对您的初步建议是尝试显式指定Content-Type响应标头:

Yii::app()->request->sendFile(
   "Youfile.pdf",
   file_get_contents('path/to/file'), 
   'application/pdf'
);
于 2020-04-23T07:34:44.683 回答
0

对于一个 1kb 的文件,听起来您的服务器在设置标头后抛出了一个 php 错误。

使用文本编辑器打开下载的文件并检查内容。

于 2015-11-24T02:06:59.723 回答