Click here to Skip to main content
15,884,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
func Uploadfile(filename, filepath string) {
    data, err := ioutil.ReadFile(filename)
    if err != nil {
        log.Fatal(err)
        os.Exit(0)
    }
    bucket, err := gridfs.NewBucket(
        CNX.Database("VWS"),
    )
    if err != nil {
        log.Fatal(err)
        os.Exit(1)
    }
    uploadStream, err := bucket.OpenUploadStream(
        filepath,
    )
    if err != nil {
        log.Fatal(err)
        os.Exit(2)
    }
    defer uploadStream.Close()

    fileSize, err := uploadStream.Write(data)
    if err != nil {
        log.Fatal(err)
        os.Exit(3)
    }
    log.Printf("Data is transferred successfully. File size is %d M\n", fileSize)
}

func main() {
	file := "tony.mp4"
	filepath := "/home/bilal/Videos"
	UploadFile(file, filepath)
}


What I have tried:

I am trying to upload the data in mongodb. But it is giving me the error that no such file or directory. Although the path is correct.
Posted
Updated 6-Jun-21 6:00am
v2

1 solution

Quote:
Although the path is correct.

The error message is clearly telling you that the path is not correct. I suspect there should be a forward slash at the beginning: "/home/bilal/Videos".
 
Share this answer
 
Comments
ibilalkayy 6-Jun-21 11:59am    
I gave the slash but still giving me the error.
Richard MacCutchan 6-Jun-21 12:13pm    
You forgot to combine the filepath and the filname in your UploadFile function. Try something like:
    data, err := ioutil.ReadFile(filepath + filename)
ibilalkayy 6-Jun-21 12:41pm    
Thank you richard!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900