Golang實(shí)現(xiàn)圖片上傳功能的示例代碼
該代碼為使用beego實(shí)現(xiàn)前后端圖片上傳。話不多說,直接上代碼。
1.前端代碼
html代碼:
<div class="col-5 f-l text text-r">背景圖(必須):</div>
<div class="img-box">
<label>
<span class="copy-btn Hui-iconfont"></span>
<input type="file" class="up-file">
</label>
</div>
<div class="img-file col-offset-5">
</div>2.JS代碼
a.讀取圖片代碼,用于在頁面上顯示
//讀取圖片
function loadImg(){
//獲取文件
var file = $(".up-file")[0].files[0];
//創(chuàng)建讀取文件的對(duì)象
var reader = new FileReader();
//創(chuàng)建文件讀取相關(guān)的變量
var imgFile;
//為文件讀取成功設(shè)置事件
reader.onload=function(e) {
var e=window.event||e;
imgFile = e.target.result;
console.log(imgFile);
$(".img-file").css({'background':"url("+imgFile+")"});
isimg();
};
//正式讀取文件
reader.readAsDataURL(file);
}b.驗(yàn)證是否有圖片存在
//是否有圖片驗(yàn)證
function isimg(){
var img= $(".img-file").css('background-image');
if(img.indexOf("data:image")==-1&&img.indexOf(".jpg")==-1){
$(".mess").html("請(qǐng)?zhí)砑颖尘皥D片").css("opacity",1);
return false;
}
else{
$(".mess").css("opacity",0).html("");
return true;
}
}c.提交上傳,將圖片以base64編碼的方式傳給后端
function addData(){
var url=$(".img-file").css("background-image");
var data1=[{"name":"url","value":url}];
$.ajax({
url:"/commmethod/method/uploadimg",
data:data1,
type:"post",
ContentType:"application/json",
success:function(resp){
......
}
});
}3.后端代碼
func (this *CommMethodController) UploadImg() {
fileurl := this.GetString("url")
DataArr := strings.Split(fileurl, ",")
//去除包裝,獲取到base64編碼
imgBase64 := DataArr[1][:len(DataArr[1])-2]
//base64轉(zhuǎn)碼
imgs, err := base64.StdEncoding.DecodeString(imgBase64)
if err != nil {
beego.Error("base64 decode error:", err)
}
timenow := time.Now().Unix()
file, err := os.OpenFile("./static/img/"+strconv.FormatInt(timenow, 10)+".jpg", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
beego.Debug("create file error:", err)
}
w := bufio.NewWriter(file) //創(chuàng)建新的 Writer 對(duì)象
_, err3 := w.WriteString(string(imgs))
if err3 != nil {
beego.Error("write error:", err3)
}
w.Flush()
defer file.Close()
imgname := strconv.FormatInt(timenow, 10) + ".jpg"
t := struct {
ImageName string `json:"imagename"`
}{imgname}
this.Data["json"] = t
this.ServeJSON()
}
/**
*此代碼主要用于編輯圖片時(shí),刪除原有圖片
* 判斷文件是否存在 存在返回 true 不存在返回false
*/
func checkFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
exist = false
}
return exist
}到此這篇關(guān)于Golang實(shí)現(xiàn)圖片上傳功能的示例代碼的文章就介紹到這了,更多相關(guān)Golang圖片上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- golang?gin框架實(shí)現(xiàn)大文件的流式上傳功能
- golang?Gin上傳文件返回前端及中間件實(shí)現(xiàn)示例
- Golang 如何限制木馬圖片上傳服務(wù)器的實(shí)例
- Golang實(shí)現(xiàn)http文件上傳小功能的案例
- golang實(shí)現(xiàn)的文件上傳下載小工具
- golang語言實(shí)現(xiàn)的文件上傳與文件下載功能示例
- Golang實(shí)現(xiàn)異步上傳文件支持進(jìn)度條查詢的方法
- Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能示例
- golang簡單獲取上傳文件大小的實(shí)現(xiàn)代碼
- golang實(shí)現(xiàn)文件上傳并轉(zhuǎn)存數(shù)據(jù)庫功能
相關(guān)文章
go?build失敗報(bào)方法undefined的解決過程
go build命令用于編譯我們指定的源碼文件或代碼包以及它們的依賴包,下面這篇文章主要給大家介紹了關(guān)于go?build失敗報(bào)方法undefined的解決過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Go使用database/sql操作數(shù)據(jù)庫的教程指南
Go?語言中,有一個(gè)名為database/sql的標(biāo)準(zhǔn)庫,提供了統(tǒng)一的編程接口,使開發(fā)人員能夠以一種通用的方式與各種關(guān)系型數(shù)據(jù)庫進(jìn)行交互,本文就來和大家講講它的具體操作吧2023-06-06
Golang中的archive/zip包的常用函數(shù)詳解
Golang 中的 archive/zip 包用于處理 ZIP 格式的壓縮文件,提供了一系列用于創(chuàng)建、讀取和解壓縮 ZIP 格式文件的函數(shù)和類型,下面小編就來和大家講解下常用函數(shù)吧2023-08-08
Golang?依賴注入經(jīng)典解決方案uber/fx理論解析
這篇文章主要為大家介紹了Golang依賴注入經(jīng)典解決方案uber/fx理論解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

