[英]How to download an uploaded file in symfony 2?
我有一個文檔實體,我希望網站的用戶能夠下載已上傳的文件。 我不知道怎么做(我在DocumentController中嘗試使用downloadAction但是我有一些錯誤)。
這是我的Document實體:
<?php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* MyBundle\Entity\Document
* @ORM\Table()
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks
*/
class Document
{
public function __toString() {
return $this->document_type->getName();
}
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var date $endDate
*
* @ORM\Column(name="endDate", type="date")
*/
private $endDate;
/**
* @ORM\ManyToOne(targetEntity="DocumentType")
* @ORM\JoinColumn(name="document_type_id", referencedColumnName="id")
*/
private $document_type;
/**
* @ORM\Column(type="string", length="255", nullable="TRUE")
* @Assert\File(maxSize="6000000")
*/
public $file;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
// do whatever you want to generate a unique name
$this->path = uniqid().'.'.$this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set endDate
*
* @param date $endDate
*/
public function setEndDate($endDate)
{
$this->endDate = $endDate;
}
/**
* Get endDate
*
* @return endDate
*/
public function getEndDate()
{
return $this->endDate;
}
/**
* Set document_type
*
* @param Was\RHBundle\Entity\DocumentType $documentType
*/
public function setDocumentType(MyBundle\Entity\DocumentType $documentType)
{
$this->document_type = $documentType;
}
/**
* Get document_type
*
* @return MyBundle\Entity\DocumentType
*/
public function getDocumentType()
{
return $this->document_type;
}
/**
* Set path
*
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set file
*
* @param string $file
*/
public function setFile($file)
{
$this->file = $file;
}
/**
* Get file
*
* @return string
*/
public function getFile()
{
return $this->file;
}
}
現在,這是我的DocumentController.php中的downloadAction:
public function downloadAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$document = $em->getRepository('MyBundle:Document')->find($id);
if (!$document) {
throw $this->createNotFoundException('Unable to find the document');
}
$headers = array(
'Content-Type' => $document->getMimeType()
'Content-Disposition' => 'attachment; filename="'.$document->getDocumentType().'"'
);
$filename = $document->getUploadRootDir().'/'.$document->getDocumentType();
return new Response(file_get_contents($filename), 200, $headers);
}
我有這個錯誤:
Call to undefined method MyBundle\Entity\Document::getMimeType()
我使用IgorwFileServeBundle 。
這是我在我的一個項目中使用的示例:
$em = $this->getDoctrine()->getEntityManager();
$file = $em->getRepository('MyBundle:File')->find($id);
$path = $file->getPath();
$mimeType = $file->getMimeType();
$folder = 'Public';
$factory = $this->get('igorw_file_serve.response_factory');
$response = $factory->create($folder.'/'.$path, $mimeType);
return $response;
我希望它可以提供幫助
這是我的一個項目的代碼
public function downloadAction()
{
$items = $this->get('xxxx')->getXXX();
$response = $this->render('xxx:xxx:xxx.csv.twig', array('items' => $items));
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename=budget.csv');
return $response;
}
將變量$ mimeType添加到Document實體
/**
* @ORM\Column()
* @Assert\NotBlank
*/
private $mimeType;
和getter和setter (或生成它們)
public function setMimeType($mimeType) {
$this->mimeType = $mimeType;
return $this;
}
public function getMimeType() {
return $this->mimeType;
}
更新 數據庫架構
php app/console doctrine:schema:update --force
並將setMimeType添加到您的setFile函數
/**
* Set file
*
* @param string $file
*/
public function setFile($file)
{
$this->file = $file;
$this->setMimeType($this->getFile()->getMimeType());
}
然后你的控制器將正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.