#Download file to folder which we created

12 messages · Page 1 of 1 (latest)

ebon hound
#

Hey guys, I am cycling xml struct and for each item in struct I want to create folder and download certain images to it. I have folder creation and downloading file done just can't understand how to download to the folder I created

#

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

tepid raven
#

the permissions are sus but it overall looks alright, what's the actual problem here?

ebon hound
#

I want to download file to the file I create

#

Now i get this

#

I expect to get

tepid raven
#

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