Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't download file #1859

Closed
letran3691 opened this issue Apr 24, 2021 · 1 comment
Closed

can't download file #1859

letran3691 opened this issue Apr 24, 2021 · 1 comment
Labels

Comments

@letran3691
Copy link

letran3691 commented Apr 24, 2021

hello
i have issue download file

download.go

func Download(c echo.Context) error {
	agr := []string{"/home/trunglv/mp3/file/"}
	list,err :=  exec.Command("ls",agr... ).Output()
	if err != nil {
		log.Printf("err %v",err)
	}
	log.Printf("ssssssss %v","/home/trunglv/file/"+string(list))
	//return c.String(http.StatusOK, string(list))
	return c.Attachment("/home/trunglv/mp3/file/"+string(list), "test.txt")
}

index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>File download</title>
</head>
<body>
<p>
    <a href="/attachment">Attachment file download</a>
</p>
</body>
</html>

main.go

func main() {
	server := echo.New()
	server.Use(middleware.Logger())
	server.Use(middleware.Recover())
	server.GET("/",handle.Welcome)
	server.GET("/attachment",handle.Download)
	server.POST("/convert",handle.GetLink)
	server.Logger.Fatal(server.Start(":8086"))	
}

after start server, i have send request server to download a file. but message log status 404 not found. i had log print file and it's exist
image

but i can't download file

everyone help my this case. thanks

@aldas aldas added the question label Apr 27, 2021
@aldas
Copy link
Contributor

aldas commented Apr 27, 2021

This is because c.Attachment first argument is path to file that is served. In your case path is wrong as exec.Command("ls",agr... ).Output() string ends with new line \n

NB: do not use exec.Comman to get directory listing or check if file exists. There are correct methods in Go standard library to do that. Using exec for these kinds of stuff will lead you to security risks.

Please read:

@aldas aldas closed this as completed Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants