@@ 5,6 5,7 @@ import (
"fmt"
"io"
"log"
+ "mime"
"net/http"
"net/url"
"os"
@@ 40,15 41,21 @@ func main() {
u, _ := url.Parse(reqURL)
fname := filepath.Base(u.Path)
- if opath != "" {
- fname = opath
- }
resp, err := http.Get(reqURL)
if err != nil {
log.Fatalln(err)
}
+ // Get from header
+ if disposition, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition")); err == nil && disposition == "attachment" {
+ fname = params["filename"]
+ }
+
+ if opath != "" {
+ fname = opath
+ }
+
ofile, oerr := os.Create(fname)
if oerr != nil {
log.Fatalln(oerr)