Basically, im trying to get the []byte from a image, using this functions:
func getAttachmentData(fileID string) ([]byte, errors.Error) {
path := fmt.Sprintf("../../../../uploads/%s", fileID)
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, errors.NewUnexpected()
}
file, err := os.Open(path)
if err != nil {
return nil, errors.NewUnexpected()
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return nil, errors.NewUnexpected()
}
fileHeader := &multipart.FileHeader{
Filename: fileInfo.Name(),
Size: fileInfo.Size(),
}
fileBytes, err := convertFileHeaderToBytes(fileHeader)
if err != nil {
return nil, errors.NewUnexpected()
}
return fileBytes, nil
}
But, when i run the code, i get an error saying that cannot find the file.
The code struct is like:
The code is in:
src/infra/repository/attach/attachment.go
and the image is in:
uploads/
I tried to use the absolute path, remove some of the “../”, change the file, but the error persists. All i want, is to make this function work fine.
user24872863 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.