<p>Scratching my head a bit here and looking for some guidance. I'm putting together a prototype app using gin-gonic that stores webform uploaded documents into a SQL backend, encoded to base64. That part seems to be going well.</p>
<p>When creating a method to enable users to download the binary documents, I am pulling the base64 data from the database, decoding to a byte array, and then trying to serve the file. Something like this:</p>
<pre><code>query := fmt.Sprintf("SELECT docblob FROM documents WHERE _id='%s'", d.ID)
var fileDataRaw string
err := db.QueryRow(query).Scan(&fileDataRaw)
checkErr(err)
fileDataBytes, err := base64.StdEncoding.DecodeString(fileDataRaw)
if err != nil {
content := gin.H{
"message": true,
"messageWarning": err.Error(),
}
c.HTML(http.StatusOK, "index.tmpl", content)
return
}
filename := "TESTING_FILE_DOWNLOAD.jpg"
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Type", "application/octet-stream")
c.File(string(fileDataBytes))
</code></pre>
<p>Example of stored data is this:</p>
<pre><code>MariaDB [mocl]> SELECT docblob FROM documents WHERE _id='20';
+--------------------------------------------------------------------------------------------------------------------------------------+
| docblob |
+--------------------------------------------------------------------------------------------------------------------------------------+
| iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAFElEQVQI12P8//8/AxJgYkAFpPIB6vYDBz/LmVgAAAAASUVORK5CYII= |
+--------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
</code></pre>
<p>When getting to <code>c.File(string(fileDataBytes))</code> I get an error 500.</p>
<p>Would someone be kind to this duffer and show me where I am getting this completely muddled up and what I should be doing?</p>
<hr/>**评论:**<br/><br/>allhatenocattle: <pre><p>I've never used gin, before but the godoc says:</p>
<pre><code>func (c *Context) File(filepath string)
File writes the specified file into the body stream in a efficient way.
</code></pre>
<p>Which sounds like it is trying to find a file named whatever the base64 string decoded to, not send those bytes. I think you might want to use something like <a href="https://godoc.org/github.com/gin-gonic/gin#Context.Data" rel="nofollow">https://godoc.org/github.com/gin-gonic/gin#Context.Data</a></p></pre>grabberfish: <pre><p>Thank you. I shall try this and report back.</p></pre>grabberfish: <pre><p>Problem half-solved! I can download small files only larger files do not download. You got me on a new track. Now I'm seeing that <code>base64.StdEncoding.DecodeString(fileDataRaw)</code> results in the following error message being produced:</p>
<pre><code>illegal base64 data at input byte 65532
</code></pre>
<p>I need to dig deeper. Thanks for your help.</p></pre>Kraigius: <pre><p>How do you encode the file to base64 and what's the type of your SQL column?</p>
<p>There's a few things that comes to mind but of course I can't really test without your full setup.</p>
<ol>
<li><p>If you are using a VARCHAR column instead of BLOB the database engine will treat the data as text and apply collation to it, which could fuck shit up.</p></li>
<li><p>You aren't encoding the same way you are decoding. ie: NoPadding vs w/ Padding</p></li>
<li><p>Maybe something unrelated to what you presented us here.</p></li>
<li><p>Some fuck up with the headers & content ie: Not POSTing a multipart/form-data or not correctly POSTing a pre-encoded file.</p></li>
<li><p>Anyways, you will want to stream the response to the client for performance reason and because it can close the connection/timeout before your server finish decoding. </p></li>
</ol></pre>metamatic: <pre><p>Why are you encoding the data to base64? Why not store <a href="https://mariadb.com/kb/en/library/blob-and-text-data-types/" rel="nofollow">BLOBs</a>?</p></pre>diabetesjones: <pre><p>I have to use Base64 to store binary data in Hashicorp Vault, for instance, as a requirement of the vault. Dunno his use case for this tho.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传