// DownloadFile looks for Content-Disposition header to find the filename attribute and returns the content-type
// header with saved file path that is saved under given downloadDir.
DownloadFile looks for Content-Disposition header to find the filename attribute and returns the content-type
header with saved file path that is saved under given downloadDir.
// DownloadFile looks for Content - Disposition header to find the filename attribute and returns the content - type // header with saved file path that is saved under given downloadDir .
func (r *Response) DownloadFile(downloadDir string) (contentType string, filePath string, err error) {
	headers := r.Headers()
	if headers == nil {
		err = errors.New("http response headers missing")
		logger.Errorf("%v", err)
		return "", "", err
	}
	contentType = headers.Get("Content-Type")
	disposition := headers.Get("Content-Disposition")
	if disposition == "" {
		err = errors.New("content-disposition header missing")
		logger.Errorf("%v", err)
		return contentType, "", err
	}
	_, params, err := mime.ParseMediaType(disposition)
	if err != nil {
		logger.Errorf("mime.ParseMediaType error: %v", err)
		return contentType, "", err
	}
	fileName := params["filename"]
	if fileName == "" {
		err = errors.New("filename missing in content-disposition")
		logger.Errorf("%v", err)
		return contentType, "", err
	}
	filePath = path.Join(downloadDir, fileName)
	err = r.SaveFile(filePath)
	if err != nil {
		logger.Errorf("cannot save file error: %v", err)
		return contentType, "", err
	}
	return contentType, filePath, nil
}
func ( r * Response ) DownloadFile ( downloadDir string ) ( contentType string , filePath string , err error ) { headers := r . Headers ( ) if headers == nil { err = errors . New ( " " ) logger . Errorf ( " " , err ) return " " , " " , err } contentType = headers . Get ( " " ) disposition := headers . Get ( " " ) if disposition == " " { err = errors . New ( " " ) logger . Errorf ( " " , err ) return contentType , " " , err } _ , params , err := mime . ParseMediaType ( disposition ) if err != nil { logger . Errorf ( " " , err ) return contentType , " " , err } fileName := params [ " " ] if fileName == " " { err = errors . New ( " " ) logger . Errorf ( " " , err ) return contentType , " " , err } filePath = path . Join ( downloadDir , fileName ) err = r . SaveFile ( filePath ) if err != nil { logger . Errorf ( " " , err ) return contentType , " " , err } return contentType , filePath , nil }
DownloadFile looks for Content-Disposition header to find the filename attribute and returns the content-type
header with saved file path that is saved under given downloadDir.
DownloadFile looks for Content - Disposition header to find the filename attribute and returns the content - type header with saved file path that is saved under given downloadDir .