- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个服务器,有一个上传图片的路由。这是一个接收一些参数的表单:标题
、描述
、可见性
和图片
。该页面还使用 Authentication
header 。
func UploadPictureRoute(prv *services.Provider) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, err := auth.ValidateRequest(prv, w, r)
if auth.RespondError(w, err) {
return
}
r.ParseMultipartForm(10 << 20) // 10 meg max
title := r.FormValue("title")
desc := r.FormValue("description")
visib := r.FormValue("visibility")
visibInt, err := strconv.Atoi(visib)
visibility := int8(visibInt) // Visibility can be either 0, 1, 2
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
file, _, err := r.FormFile("picture")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
defer file.Close()
mimeType, _, err := mimetype.DetectReader(file) // Package gabriel-vasile/mimetype
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if !utils.IsValidMimetype(mimeType) { // Basically just comparing to image/png, image/jpg. Crashes here
w.WriteHeader(http.StatusBadRequest)
return
}
parentFolder := prv.PicturePath + "/" + strconv.FormatInt(*user.ID, 10) + "/"
_, err = os.Stat(parentFolder)
if os.IsNotExist(err) {
err = os.MkdirAll(parentFolder, os.ModePerm)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
pict := model.Picture{
Title: title,
Description: desc,
Creator: &user,
Visibility: visibility,
Ext: utils.GetExtForMimetype(mimeType),
}
pict, err = dal.CreatePicture(prv, pict)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
outputFile, err := os.Create(parentFolder + strconv.FormatInt(*pict.ID, 10) + "." + pict.Ext)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
dal.DeletePicture(prv, pict)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, file)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Responding
}
}
使用 Postman
效果很好,它可以正确上传文件,一切都按预期进行。
但是,我未能编写一个 go http
客户端来上传文件:
type uploadResponse struct {
URLID string
}
func main() {
filename := "filename.png"
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
}
defer file.Close()
// Just to be sure we're really at the start of the file
_, err = file.Seek(0, io.SeekStart)
if err != nil {
fmt.Println("Can't read the file")
return
}
mime, _, err := mimetype.DetectReader(file)
if err != nil {
fmt.Println("Can't read the file")
return
}
if !api.IsMimetypeAllowed(mime) { // Just check if it's a jpg/png/gif. This works on the client
fmt.Println("This filetype can't be uploaded to a Scinna server.")
fmt.Println("Please convert this file to jpeg, png or gif.")
return
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
title, _ := writer.CreateFormField("title")
title.Write([]byte("My picture title"))
desc, _ := writer.CreateFormField("description")
desc.Write([]byte("My picture description"))
visib, _ := writer.CreateFormField("visibility")
visib.Write([]byte("0"))
pict, _ := writer.CreateFormFile("picture", file.Name())
_, err = io.Copy(pict, file)
writer.Close()
r, err := http.NewRequest("POST", "https://myapi.local/pictures", body)
if err != nil {
panic(err)
}
r.Header.Add("Content-Type", writer.FormDataContentType())
r.Header.Add("Authorization", "Bearer "+config.Token)
client := &http.Client{}
client.Do(r)
}
此代码会导致服务器崩溃。图片的 mimetype 变为 application/octet-stream
并且图像标题已损坏(它仍然在某些编辑器中打开,但 EyesOfGnome 基本上表示该图片不是 JPG/PNG 文件,因为它可以'找不到开头的神奇数字)
如何修复HTTP go客户端才能成功上传图片?
最佳答案
对 mimetype.DetectReader(file)
的调用读取文件的一部分。对 _, err = io.Copy(pict, file)
的调用会读取文件的其余部分。要读取整个文件,请返回到调用 io.Copy 之前的偏移量 0。
文件在偏移量 0 处打开。调用 Open
后无需立即查找偏移量 0。
通过交换调用顺序来修复问题:
...
mime, _, err := mimetype.DetectReader(file)
if err != nil {
fmt.Println("Can't read the file")
return
}
// Rewind to the start of the file
_, err = file.Seek(0, io.SeekStart)
if err != nil {
fmt.Println("Can't read the file")
return
}
...
服务器也有类似的问题。检测类型后回退:
mimeType, _, err := mimetype.DetectReader(file) // Package gabriel-vasile/mimetype
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Rewind to the start of the file
_, err = file.Seek(0, io.SeekStart)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
关于http - 使用 mime/multipart 上传会损坏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59028602/
如果上传的文件没有扩展名,是否必须指定 MIME 类型?换句话说,是否有默认的通用 MIME 类型? 最佳答案 对于未知类型,您可以使用application/octet-stream。 RFC 20
MIME 类型的官方 URN 是否存在? Mozilla Firefox 和其他应用程序使用诸如“urn:mimetype:text/plain”或“urn:mimetype:handler:text
我们有一个由 xyz 网站管理员部署在生产环境中的网站。以下是在两个不同实例中发生错误时 Web 控制台的两个屏幕截图。 我们时断时续地而不是始终如一地面对这个问题。此外,这种情况发生在少数用户而不是
此错误仅在 chrome 中显示。我正在搜索这个问题,但还没有找到 令人满意的解决方案。我附上了该错误的屏幕截图。请为此提出一些想法。 Error image 最佳答案 样式表的 MIME 类型应该是
当我使用 MAMP 托管我的 Drupal 站点时,出现了上述错误。我所有的 HTML 加载,但我的 CSS 没有加载。 拒绝应用来自“http://mysite/sites/default/file
错误信息: jquery.dataTables.min.js:23 未捕获类型错误:无法读取未定义的属性“长度” 在 Ha (jquery.dataTables.min.js:23) 在 HTMLTa
Raw images由相机、扫描仪等生成,通常比 JPEG 图像具有更大的后处理灵活性。 如果我从我的 Web 服务器提供这种文件,我应该使用哪种媒体类型(又名 MIME 类型)? 我对最近的佳能相机
我已经查看了大约 100 个站点,但没有找到任何内容。有些只是 mime 类型值的列表,有些只是对 RFC 的引用,或者或多或少是直接引用它们。 RFC 是难以理解的,每一个都只揭示了大象的一小部分。
我想用 image/*数据 URI 中的 MIME 类型,例如data:image/*;base64,R0lGODlhE...在 元素。 它是否完全符合标准的 MIME 类型? 它会在某些浏览器中引起
我有一个 PHP 脚本,用于下载不同类型的文件(PDF、JPEG、RAR 等)。效果很好,但是我注意到 MIME 类型存在问题。 是否有我可以安全地用于“通用目的”的通用 MIME 类型?我正在考虑所
我的程序使用自己的二进制文件类型,因此我假设我不能使用 MIME 类型文本/纯文本,因为它不是 7 位 ASCII 文件。 我应该将其命名为“application/myappname”吗? 最佳答案
我正在调试一个 MIME 解析器,它无法解析来自一个电子邮件服务的 header 。电子邮件服务包含一个 header ,该 header 由完全位于下一行的字段主体组成,如下所示: Message-
我有一个 Vue.js 项目,当我检查控制台时发现以下问题: Refused to apply style from 'http://localhost:8080/dist/cropper.min.c
friend 们,我花了一整天的时间寻找这个问题的解决方案,但我一直无法修复它。当我为生产构建它时,在开发模式下(没有缩小和其他东西)它在本地主机上运行良好,但是当我在 dist 文件夹中获取我的文件
Swagger 默认支持 application/json 作为 Content 类型。如果我想添加其他内容类型,则需要在 swagger JSON 文件中更新“生产”或“消费”字段。客户可以发送任何
我的网络服务生成并返回一个 .tex 文件。我想在 http-header 中提供正确的 mimetype。 除了here我没找到这么多,我在其中找到 application/x-tex。这是要坚持的
我需要区分不同类型的 HTML 模板,例如来自 Handlebars 模板的 FreeMarker。由于模板在数据库中(所以它们不是文件,即没有文件名或扩展名)我想使用 MIME 类型来识别它们。 对
我想知道 iWork 页面的 mime 类型是什么?以及 iWork 套件中其余软件的 mime 类型是什么?我在网上环顾四周,没有看到任何地方。 最佳答案 我最近需要这个来工作,结果只是上传了一些文
我正在构建一个 mime 类型的猜测器,我正在使用 mimetype命令来检查我的结果。对于日志文件,它返回 text/x-log .我在这些列表中没有看到这种 mime 类型: http://svn
有谁知道 Markdown 是否存在 MIME 类型?我猜是 text/plain,但是有更具体的吗? 最佳答案 tl;dr:text/markdown 自 2016 年 3 月起 2016 年 3
我是一名优秀的程序员,十分优秀!