我查看了官方文档http://symfony.com/doc/2.8/cookbook/controller/upload_file.html和其他主题,但无法找到解决方案
所以,这是我的角色动作
$scope.uploadFile = function(files) {
var fd = new FormData();
fd.append("file", files[0]);
$http.put('/api/clients/' + id +'/files', fd, {
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).success(function(resp) {
$mdToast.show(
$mdToast.simple()
.textContent(resp.message)
.hideDelay(1500)
);
}).error(function(resp) {
$mdToast.show(
$mdToast.simple()
.textContent(resp.message)
.hideDelay(1500)
);
})
};
我的上传文件实体
namespace PT\HearingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class Client
*
* @ORM\Entity(repositoryClass="PT\HearingBundle\Repository\ClientRepository")
* @ORM\Table(name="`client_uploads`")
*
* @package PT\HearingBundle\Entity
* @author Roman Lunin <roman.lunin@primary.technology>
*/
class ClientUploads
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @JMS\Groups("list")
*/
protected $id;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
* @Assert\File(mimeTypes={ "application/pdf" })
*/
private $files;
/**
* @ORM\ManyToOne(targetEntity="Client", inversedBy="files")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
* @JMS\Groups("list")
*
* @var Client
*/
protected $client;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get client
*
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* Get files
*
* @return string
*/
public function getFiles()
{
return $this->files;
}
/**
* Set files
*
* @param string $files
*
* @return ClientUploads
*/
public function setFiles($files)
{
$this->files = $files;
return $this;
}
/**
* Set client
*
* @param int $client
* @return Client
*/
public function setClient($client)
{
$this->client = $client;
return $this;
}
}
它的形式
class ClientUploadsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('files')
->add('files', FileType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'PT\HearingBundle\Entity\ClientUploads'
));
}
}
而且,好吧,我无法得到我应该放入symfony控制器的内容,现在它看起来像那样,并且根本不起作用。
public function putClientFilesAction(Request $request, $id)
{
$upload = new ClientUploads();
$form = $this->createForm(ClientUploadsType::class, $upload);
$form->handleRequest($request);
var_dump($request->files->get('file'));
exit;
$em = $this->getDoctrine()->getEntityManager();
if ( ! $form->isValid()) {
return new JsonResponse(array(
'message' => 'File is not valid'
), Response::HTTP_BAD_REQUEST);
}
$file = $upload->getFiles();
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
$file->move(
$this->container->getParameter('files_directory'),
$fileName
);
$upload->setClient($id);
$upload->setFiles($fileName);
$em->flush();
return new JsonResponse(array(
'message' => 'File saved'
), Response::HTTP_OK);
}