在我的golang项目中,我需要删除一个用户下载的文件,我使用了defer,但是下载后没有调用它,所以文件没有被删除,代码如下:
func (h *Handler) Download(c *fiber.Ctx) error {
filename := c.Query("filename")
c.Attachment(filename)
tempFile := filepath.Join(os.TempDir(), filename)
//Open the file for reading
file, err := os.Open(tempFile)
if err != nil {
return err
}
//delete the file when everything is done, not working
defer os.Remove(tempFile)
//read the file
b, err := ioutil.ReadAll(file)
if err != nil {
return er.Wrap(err)
}
//close the file explicitly after reading
err = file.Close()
if err != nil {
return er.Wrap(err)
}
//also I tried to delete the file explicitly but keep getting error saying that the file is being used by another process
//os.Remove(tempFile)
//send the file to user
return c.Send(b)
}
有什么好办法都欢迎。谢谢。
1条答案
按热度按时间xu3bshqb1#
mybe os.Remove返回错误,例如,tempFile的路径为错误
使用以下代码查看错误