mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-07 12:31:26 +08:00
46 lines
798 B
Go
46 lines
798 B
Go
package utilFile
|
||
|
||
import (
|
||
"io"
|
||
"mime/multipart"
|
||
"net/http"
|
||
"os"
|
||
)
|
||
|
||
// DownloadFile 会将url下载到本地文件,它会在下载时写入,而不是将整个文件加载到内存中。
|
||
func DownloadFile(url, filepath string) error {
|
||
// Get the data
|
||
resp, err := http.Get(url)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
// Create the file
|
||
out, err := os.Create(filepath)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer out.Close()
|
||
// Write the body to file
|
||
_, err = io.Copy(out, resp.Body)
|
||
return err
|
||
}
|
||
|
||
func SaveUploadedFile(file *multipart.FileHeader, dst string) error {
|
||
src, err := file.Open()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer src.Close()
|
||
|
||
out, err := os.Create(dst)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer out.Close()
|
||
|
||
_, err = io.Copy(out, src)
|
||
return err
|
||
}
|