首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跟踪文件下载

跟踪文件下载
EN

Stack Overflow用户
提问于 2012-01-18 15:58:11
回答 2查看 386关注 0票数 1

我想在symfony2中创建一个区域,其中包含用户可以下载的文件列表。

我希望能够跟踪每个文件被下载的次数。

有谁能解释一下我是如何做到这一点的?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-18 23:49:03

以简单的方式在你的文件列表中,你必须设置你的控制器的URL,它将计算下载的次数,然后打印直接链接到文件。

代码语言:javascript
复制
{# list of files in template %}
{% for file in files %}
    <a href="{% path('counting', {'id': file.id}) %}">{{ file.name }}</a>
{% endfor %}

// Controller
/**
 * Counting the number of file downloads
 *
 * @Route("/counting/{id}", name="counting", requirements={"id" = "\d+"})
 * @Template()
 */
public function countingAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $file = $em->getRepository('MyBundle:File')->find($id);

    $count = $file->getCount();
    $file->setCount($count + 1);

    $em->persist($entity);
    $em->flush();

    return array('file' => $file);
}

{# final file page in template #}
<a href="{{ file.url }}">{{ file.name }}</a>
票数 1
EN

Stack Overflow用户

发布于 2012-01-18 17:05:53

使用Doctrine实体-本文"How to handle file upload“中提供了一些信息

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8906895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档