이 름 : 바다아이
|
조회수 : 12161
짧은 주소 : https://www.bada-ie.com/su/?221591827644
mm 이라는 맵으로 업로드 정보를 받아서 나중에 해당 정보를 디비에 넣으시면 됩니다.
정리가 안된 부분은 입맛에 맞게 고쳐쓰세요...
funcErrorCon(err error) {
if err != nil {
log.Print(err)
}
}
funcErrorCon2(w http.ResponseWriter, msg string) {
list := `<html><body><script>window.alert('` + msg + `'); history.go(-1);</script></body></html>`
io.WriteString(w, list)
return
}
funcStringInSlice(str string, list []string)bool {
for _, v := range list {
if strings.EqualFold(v, str) {
returntrue
}
}
returnfalse
}
funcNotfile(file string)bool {
i := []string{"html", "php", "js", "htm", "inc", "jsp", "asp", "go"}
var t []stringvar b bool
t = strings.Split(file, ".")
b = StringInSlice(t[len(t)-1], i)
return b
}
funcRandInt(min int, max int)int {
return min + rand.Intn(max-min)
}
funcFileUpload(w http.ResponseWriter, req *http.Request, dir string)map[string]string {
mm := make(map[string]string)
var size int64 = 10 * 1024 * 1024
err := req.ParseMultipartForm(size)
if err != nil {
ErrorCon(err)
}
files := req.MultipartForm.File["userfile[]"]
for i, _ := range files {
if Notfile(files[i].Filename) {
ErrorCon2(w, "올리면 안되는 파일 입니다.")
returnfalse, mm
}
}
for i, _ := range files {
if files[i].Filename != "" {
file, err := files[i].Open()
if err != nil {
ErrorCon(err)
}
t := strings.Split(files[i].Filename, ".")
t2 := t[len(t)-1]
ti := time.Now()
d2 := ti.Unix()
tmpRand := strconv.Itoa(RandInt(1, 1000000000)) + `_` + strconv.FormatInt(d2, 10)
save := dir + `/` + tmpRand + `.` + t2
//create destination file making sure the path is writeable.
dst, err := os.Create(save)
if err != nil {
ErrorCon(err)
}
//copy the uploaded file to the destination fileif _, err = io.Copy(dst, file); err != nil {
ErrorCon(err)
}
// 실제 파일 이름이 키값, 복사되는 가상 파일이름이 value
mm[files[i].Filename] = tmpRand + `.` + t2
요기다는 그림파일이면 썸네일 굽기처리 만드세요
dst.Close()
file.Close()
}
}
return mm
}
|
|
번 호
카테고리
제 목
이름
조회수
Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천 실행시간 : 0.25424 초 to webmaster... gogo sea. gogo sea.