Silverstripe 3.1.5 - Upload Error SyntaxError: Unexpected token <

丶灬走出姿态 提交于 2019-12-01 09:11:00

Silverstripe (3.1) will not allow you to save a svg file as an Image data type. It may be to do with the PHP GD library (I'm not sure), which the Silverstripe Image class uses.

Instead, you can save a svg file as a File data type.

To do this all you need is to add the svg file type to the File allowed_extensions in your yml config file (as you posted in your questions):

File: 
  allowed_extensions:
    - svg

In your Page or DataObject class add the File relation and set your UploadField:

private static $has_one = array(
    'SVGFile' => 'File'
);

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

    $fields->addFieldToTab('Root.SVG', UploadField::create('SVGFile', 'SVG File'));

    return $fields;
}

In your page template you can use the file URL to load the svg as you like.

Here's a module to have SVG work as images in SilverStripe: https://github.com/micschk/silverstripe-svg-images/

See the README for general pointers on how to set up SVG-as-image if you don't want to require the module.

The problem "SyntaxError: Unexpected token <" is caused by an error being returned via HTML instead of a data stream in my experience. So I'd look at the contents of the network tab response and expect to see a stack trace with an error.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!