Angularjs 使用Symfony 2和Angular.js上传文件
我已经查看了官方文档和其他主题,但找不到解决方案 这是我的角度动作Angularjs 使用Symfony 2和Angular.js上传文件,angularjs,symfony,file-upload,Angularjs,Symfony,File Upload,我已经查看了官方文档和其他主题,但找不到解决方案 这是我的角度动作 $scope.uploadFile = function(files) { var fd = new FormData(); fd.append("file", files[0]); $http.put('/api/clients/' + id +'/files', fd, { withCredentials: t
$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;
}
}
而且,好吧,我不能得到我应该放在我的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);
}
您的后端似乎还可以,您是否检查了请求主体并查看文件是否在那里?您可能希望看到我的工作实现
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);
}