Pro*_*mer 2 api image file download go
我正在 golang 中构建一个简单的测试 API,用于上传和下载图像文件(PNG、JPEG、JPG):
/pic [POST] 用于上传图像并将其保存到文件夹中;/pic [GET] 用于将图像下载到客户端。
我已成功构建 /pic [POST] 并且图像已成功上传到服务器的文件。我可以打开存储文件夹中的文件。(在 windows localhost 服务器和 ubuntu 服务器中)
但是,当我构建 /pic [GET] 用于下载图片时,我可以将文件下载到客户端(我的计算机),但是下载的文件在某种程度上已损坏,因为当我尝试使用不同的图像查看器打开它时,例如作为画廊或 Photoshop,它说“看起来我们不支持这种文件格式”。所以看起来下载不成功。
邮递员结果:
在图库中打开文件:
关于为什么会发生这种情况以及我应该如何解决它的任何想法?
下载图片的golang代码如下(省略错误处理):
func PicDownload(w http.ResponseWriter, r *http.Request){
request := make(map[string]string)
reqBody, _ := ioutil.ReadAll(r.Body)
err = json.Unmarshal(reqBody, &request)
// Error handling
file, err := os.OpenFile("./resources/pic/" + request["filename"], os.O_RDONLY, 0666)
// Error handling
buffer := make([]byte, 512)
_, err = file.Read(buffer)
// Error handling
contentType := http.DetectContentType(buffer)
fileStat, _ := file.Stat()
// Set header
w.Header().Set("Content-Disposition", "attachment; filename=" + request["filename"])
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.FormatInt(fileStat.Size(), 10))
// Copying the file content to response body
io.Copy(w, file)
return
}