我正在尝试提供带有SVG属性的HTML页面,所以只要单击"Create“,我就希望能够以.jpg而不是SVG的形式下载该文件。我查看了多个与命令行配合工作的转换器,例如,如下所示。
os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")简单地说,我希望能够点击create并下载我的文件的.jpg版本。我目前正在尝试使用Flask来做这件事。
without_filter = <svg viewBox="0 0 400 150" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="background: #40484b;">
<defs>
<!-- if you use the same shape multiple times, you put the reference here and reference it with <use> -->
<rect id="path-1" x="25" y="25" width="100" height="100"></rect>
<rect id="path-3" x="150" y="25" width="100" height="100"></rect>
<rect id="path-5" x="275" y="25" width="100" height="100"></rect>
<!-- gradient -->
<linearGradient x1="50%" y1="0%" x2="50%" y2="100%" id="gradient">
<stop stop-color="#FF8D77" offset="0%"></stop>
<stop stop-color="#FFBDB1" offset="50%"></stop>
<stop stop-color="#F4E3F6" offset="100%"></stop>
</linearGradient>
<!-- clip-path -->
<clipPath id="clip">
<circle cx="325" cy="75" r="50"></circle>
</clipPath>
<!-- filter -->
<filter x="-50%" y="-50%" width="200%" height="200%" filterUnits="objectBoundingBox" id="glow">
<feGaussianBlur stdDeviation="15" in="SourceGraphic">
<animate attributeName="stdDeviation" attributeType="XML"
begin="0s" dur="0.25s" repeatCount="indefinite"
values="15;20;15"/>
</feGaussianBlur>
</filter>
</defs>
<g fill-rule="evenodd">
<use xlink:href="#path-1" filter="url(#glow)" fill="#FF8D77" fill-opacity="0.5"></use>
<use xlink:href="#path-3" fill="url(#gradient)"></use>
<use xlink:href="#path-5" fill="#FF8D77" clip-path="url(#clip)"></use>
</g>
</svg>因此,只要用户单击"create“,我就尝试下载(html)作为.jpeg。这是我的视图函数。
@app.route('/filters/<filename>', methods=['GET','POST'])
def AddFiltersTag(filename):
form = AddFilterTags(request.form)
if form.validate_on_submit():
html = re.sub(r'(.*?<defs>)(.*)', r'\g<1>' + add_filter_tag + add_filter + end_tag + '\g<2>', without_filter)
save_html = open('save.svg','wb')
save_html.write(html)
os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")
return download_html因此,当用户在我的表单上单击"Submit“时,函数AddFilterTag就会被触发。但是在我想要下载的情况下,上面的代码只是将文件另存为.svg,并在稍后使用os.system进行转换,但我实际上想直接下载.jpg版本。有没有办法做到这一点?
发布于 2016-10-01 17:55:45
在保存文件并将其转换为图像之后。
您可以直接从目录返回文件,这将为用户提供一个下载弹出窗口。
...
os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")
return send_from_directory("/path/to/saved/image", "icon-32.jpg")请确保导入方法,如下所示:
from flask import send_from_directory如果你在生产中使用它,请确保nginx或实际的web服务器正在发送该文件。你可以在http://flask.pocoo.org/docs/0.11/api/#flask.send_from_directory上阅读更多
https://stackoverflow.com/questions/39802098
复制相似问题