#Download file to folder which we created
12 messages · Page 1 of 1 (latest)
My download:
func downloadFile(URL, fileName string) error {
//Get the response bytes from the url
response, err := http.Get(URL)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errors.New("Received non 200 response code")
}
//Create a empty file
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
//Write the bytes to the fiel
_, err = io.Copy(file, response.Body)
if err != nil {
return err
}
return nil
}
This code is hard draft I will go rewrite this later on. Just want to get it to work and work on improvements later
Creating folder via go if err := os.Mkdir(v.ProductNo2, os.ModePerm); err != nil { fmt.Println(err) } for now
the permissions are sus but it overall looks alright, what's the actual problem here?
So basically now it downlaods file to the file from which program is ran
I want to download file to the file I create
Now i get this
I expect to get
Ye will fix permissions xD
if you want to create a file at /a/b/c/d.txt you just need to arrange it so that you call Create with /a/b/c/d.txt. Putting path traversal attacks aside you can filepath.Join("/a/b/c", "d.txt") for this effect