Base64 decode & binary file delivery question

agolangf · · 869 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Scratching my head a bit here and looking for some guidance. I&#39;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(&#34;SELECT docblob FROM documents WHERE _id=&#39;%s&#39;&#34;, d.ID) var fileDataRaw string err := db.QueryRow(query).Scan(&amp;fileDataRaw) checkErr(err) fileDataBytes, err := base64.StdEncoding.DecodeString(fileDataRaw) if err != nil { content := gin.H{ &#34;message&#34;: true, &#34;messageWarning&#34;: err.Error(), } c.HTML(http.StatusOK, &#34;index.tmpl&#34;, content) return } filename := &#34;TESTING_FILE_DOWNLOAD.jpg&#34; c.Header(&#34;Content-Description&#34;, &#34;File Transfer&#34;) c.Header(&#34;Content-Transfer-Encoding&#34;, &#34;binary&#34;) c.Header(&#34;Content-Disposition&#34;, &#34;attachment; filename=&#34;+filename) c.Header(&#34;Content-Type&#34;, &#34;application/octet-stream&#34;) c.File(string(fileDataBytes)) </code></pre> <p>Example of stored data is this:</p> <pre><code>MariaDB [mocl]&gt; SELECT docblob FROM documents WHERE _id=&#39;20&#39;; +--------------------------------------------------------------------------------------------------------------------------------------+ | 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&#39;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&#39;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&#39;s the type of your SQL column?</p> <p>There&#39;s a few things that comes to mind but of course I can&#39;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&#39;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 &amp; 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

869 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传