如何为用户上传到后端的DataObject的文件添加下载按钮?

时间:2015-11-29 06:47:31

标签: silverstripe

我想在我<?php if(!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['order'])) { $data = array( 'message' => "All fields are required." ); echo json_encode($data); } ?> 的后端添加一个下载按钮。

我在前端有一个上传表单,用户可以上传文件。例如.jpg或.pdf。

void addToEnd(PlayerListNode *newNode){

    if (newNode == NULL) {
        printf("ERROR");
        exit(1);

    }
    printf("\n Inserting %d ... \n", newNode->player);
    PlayerListNode *current = listHead;
    while (current -> next != NULL)
    {
        current = current->next;
    }
    current->next = newNode;
}

现在在后端,当您从DataObject点击进入各个条目时,我希望能够下载用户上传的文件。是否可以在&#34;选择另一个文件&#34;旁边添加一个按钮。按钮或创建一个位于该部分按钮下方的按钮:

enter image description here

ContactSubmission.php

class TeamPage_Controller extends Page_Controller
{
    private static $allowed_actions = array(
        'ContactForm'
        );

        $fields = new FieldList(array(
            TextField::create('Email'),
            FileField::create('MapFile')
        ));

        $actions = new FieldList(
            FormAction::create("doSaveSubmission")
        );

        $required = new RequiredFields('Email');

        $form = new Form($this, 'ContactForm', $fields, $actions, $required);

        return $form;


    }

    public function doSaveSubmission($data, $form){

        $submission = new ContactSubmission();
        $form->saveInto($submission);
        $submission->TeamPageID = $this->ID;
        $submission->write();

        return 'Saved into Dataobject';

    }




}

2 个答案:

答案 0 :(得分:2)

我们可以通过为UploadField创建新模板来自定义UploadField按钮。它使用的默认模板是UploadField_FileButtons。我们将复制此内容以创建新模板。

我们在 mysite / templates / includes 中创建名为 UploadField_FileButtonsWithDownload 的模板。在此模板中,我们复制原始UploadField_FileButtons并在最后添加我们的下载链接。

<强> UploadField_FileButtonsWithDownload.ss

<% if $canEdit %>
    <button class="ss-uploadfield-item-edit ss-ui-button ui-corner-all" title="<% _t('UploadField.EDITINFO', 'Edit this file') %>" data-icon="pencil">
    <% _t('UploadField.EDIT', 'Edit') %>
    <span class="toggle-details">
        <span class="toggle-details-icon"></span>
    </span>
    </button>
<% end_if %>
<button class="ss-uploadfield-item-remove ss-ui-button ui-corner-all" title="<% _t('UploadField.REMOVEINFO', 'Remove this file from here, but do not delete it from the file store') %>" data-icon="plug-disconnect-prohibition">
<% _t('UploadField.REMOVE', 'Remove') %></button>
<% if $canDelete %>
    <button data-href="$UploadFieldDeleteLink" class="ss-uploadfield-item-delete ss-ui-button ui-corner-all" title="<% _t('UploadField.DELETEINFO', 'Permanently delete this file from the file store') %>" data-icon="minus-circle"><% _t('UploadField.DELETE', 'Delete from files') %></button>
<% end_if %>
<% if $UploadField.canAttachExisting %>
    <button class="ss-uploadfield-item-choose-another ss-uploadfield-fromfiles ss-ui-button ui-corner-all" title="<% _t('UploadField.CHOOSEANOTHERINFO', 'Replace this file with another one from the file store') %>" data-icon="network-cloud">
    <% _t('UploadField.CHOOSEANOTHERFILE', 'Choose another file') %></button>
<% end_if %>

<a class="ss-ui-button ui-corner-all" title="Download this file" href="$Link" target="_blank" download>Download</a>

然后,我们为UploadField字段设置MapFile模板,以使用我们的新模板。

<强> ContactSubmission.php

class ContactSubmission extends DataObject {

    private static $db = array(
        'Email' => 'Varchar(255)'
    );

    private static $has_one = array(
        'TeamPage' => 'TeamPage',
        'MapFile' => 'File'
    );


    private static $summary_fields = array(
        'Email'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        if ($mapFileField = $fields->fieldByName('Root.Main.MapFile')) {
            $mapFileField->setTemplateFileButtons('UploadField_FileButtonsWithDownload');
        }

        return $fields;
    }

}

现在,CMS中的选择其他文件按钮后应显示下载按钮链接。

答案 1 :(得分:0)

您可以在DataObject表单上添加新操作。

要执行此操作,请查看Better Buttons模块。 https://github.com/unclecheese/silverstripe-gridfield-betterbuttons

在操作本身中,您需要让SilverStripe返回带有forcedownload标头的文件。你可以看到别人在这里尝试这个.. http://www.silverstripe.org/community/forums/general-questions/show/15832