如何在Golang中从Base64编码的文件中提取文件扩展名?

huangapple go评论78阅读模式
英文:

How to extract file extension from base64 encoded file in golang?

问题

我想允许用户上传以Base64编码的文件。结果可能如下所示:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kA...

或者

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfAAAAAXNSR0IArs4c6QAAAARnQU...

所以我想知道从编码的文件字符串中提取文件扩展名的惯用方法是什么?

英文:

I want to allow users to upload files as base64 encoded.
The results are like:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kA...

or

  data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfAAAAAXNSR0IArs4c6QAAAARnQU...

So I'm wondering what is the idomatic way to extract the file extension from the encoded file strings?

答案1

得分: 2

使用字符串/字节函数很容易提取 MIME 类型,即位于 data:;base64 之间的部分。

然后,您可以使用标准的 mime 包从类型中获取扩展名:
https://golang.org/pkg/mime/#ExtensionsByType

英文:

It's simple enough to use string/byte functions to extract the mime type — i.e., the part between data: and ;base64.

Then you can use the standard mime package to get the extension from the type:
https://golang.org/pkg/mime/#ExtensionsByType

答案2

得分: 0

有一个很好的 Golang 包似乎在 GitHub 上有,它提供了关于 base64 图像 URI 字符串的完整信息。链接是 https://github.com/vincent-petithory/dataurl

英文:

There is a good golang package seems to be there on github https://github.com/vincent-petithory/dataurl that gives whole information about a base64 image URI string.

huangapple
  • 本文由 发表于 2017年8月16日 18:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/45710875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定