iedc-go/vendor/code.hoteas.com/golang/hotime/dri/download/download.go

41 lines
720 B
Go
Raw Permalink Normal View History

2023-03-02 19:12:15 +00:00
package download
import (
"bytes"
. "code.hoteas.com/golang/hotime/common"
"io"
"io/ioutil"
"net/http"
"os"
)
// Down 下载文件
func Down(url, path, name string, e ...*Error) bool {
os.MkdirAll(path, os.ModeDir)
if Substr(path, len(path)-1, len(path)) != "/" {
path = path + "/"
}
out, err := os.Create(path + name)
if err != nil && len(e) != 0 {
e[0].SetError(err)
return false
}
defer out.Close()
resp, err := http.Get(url)
if err != nil && len(e) != 0 {
e[0].SetError(err)
return false
}
defer resp.Body.Close()
pix, err := ioutil.ReadAll(resp.Body)
_, err = io.Copy(out, bytes.NewReader(pix))
if err != nil && len(e) != 0 {
e[0].SetError(err)
return false
}
return true
}