登录
首页 >  Golang >  Go问答

图像没有记录在mysql数据库中

来源:stackoverflow

时间:2024-04-11 13:51:35 352浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《图像没有记录在mysql数据库中》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

我想将数据从表单写入数据库(不同的表),但由于某种原因,图像没有记录。

func save_obj(w http.responsewriter, r *http.request) {
    title := r.formvalue("title")
    type_obj := r.formvalue("type_obj")
    location := r.formvalue("location")
    long := r.formvalue("long")
    fond := r.formvalue("fond")
    //video := r.formvalue("video")
    inf := r.formvalue("inf")
    pros := r.formvalue("pros")
    about := r.formvalue("about")
    //docs := r.formvalue("docs")
    //подключение
    db, err := sql.open("mysql", "root:root@tcp(127.0.0.1:8889)/service")
    if err != nil {
        panic(err)
    }

    defer db.close()

    //установка
    insert, err := db.query(fmt.sprintf("insert into `objects` (`title`, `type_obj`, `location`, `long`, `fond`, `inf`, `pros`, `about`)"+
        " values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", title, type_obj, location, long, fond, inf, pros, about))
    if err != nil {
        panic(err)
    }
    defer insert.close()
    imgone := b64.urlencoding.decodestring(r.formvalue("img-1"))
    imgtwo := b64.urlencoding.decodestring(r.formvalue("img-2"))
    imgthree := b64.urlencoding.decodestring(r.formvalue("img-3"))
    imgfour := b64.urlencoding.decodestring(r.formvalue("img-4"))
    ins, er := db.query(fmt.sprintf("insert into `img-1` (`title`, `img`) values('%s', '%s')", title, imgone))
    if er != nil {
        panic(er)
    }
    defer ins.close()
    insr, error := db.query(fmt.sprintf("insert into `img-2` (`title`, `img`) values('%s', '%s')", title, imgtwo))
    if error != nil {
        panic(error)
    }
    defer insr.close()
    in, error := db.query(fmt.sprintf("insert into `img-3` (`title`, `img`) values('%s', '%s')", title, imgthree))
    if error != nil {
        panic(error)
    }
    defer in.close()
    //последнее изображение
    in, error := db.query(fmt.sprintf("insert into `img-4` (`title`, `img`) values('%s', '%s')", title, imgfour))
    if error != nil {
        panic(error)
    }
    defer in.close()
    http.redirect(w, r, "/create_obj", http.statusseeother)
}

除了图像录制之外,一切都正常,但是当我尝试录制时,会显示错误:

无法在 base64.urlencoding.encodetostring 的参数中使用 r.formvalue("img-1") (字符串类型)作为 []byte 类型 html:





为什么类型不匹配以及如何加密 blob 中的图像?


解决方案


根据错误, r.formvalue("img-1") (也可能是 img-2img-3)的类型已经是 stringbase64.URLEncoding.EncodeToString() 采用 []byte 参数。

由于您的表单使用表单类型 file,因此您需要将其处理为 multipart 并使用 r.FormFile("img-1") 获取图像,如下所示以获取字节:

file, header, err := r.FormFile("img-1")    
defer file.Close()
if err != nil {
    // handle error
}
b := bytes.NewBuffer(nil)
if _, err := io.Copy(b, file); err != nil {
    // handle error
}
// convert bytes to base64
img := base64.URLEncoding.EncodeToString(b.Bytes())

您可以利用此函数创建一个方便的函数,该函数采用 fileheader。在 header 中,您可以找到文件名、大小和 mime/类型等元数据,这些元数据可以与图像一起存储到数据库以供进一步使用。

终于介绍完啦!小伙伴们,这篇关于《图像没有记录在mysql数据库中》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>