Skip to content

Instantly share code, notes, and snippets.

@wyyqyl
Created April 29, 2014 13:24
Show Gist options
  • Save wyyqyl/11400280 to your computer and use it in GitHub Desktop.
Save wyyqyl/11400280 to your computer and use it in GitHub Desktop.
Download .crx file from web server, and don't let chrome display yellow bar at the top of the page
package main
import (
"log"
"net/http"
"os"
"strconv"
)
func handler(w http.ResponseWriter, r *http.Request) {
file_name := r.URL.Path[1:]
log.Println(file_name)
if len(file_name) == 0 {
log.Println("file_name is empty")
return
}
file, err := os.Open(file_name)
if err != nil {
log.Println("Unable to open file", file_name)
return
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
log.Println("Unable to get file size")
return
}
file_size := stat.Size()
bs := make([]byte, file_size)
_, err = file.Read(bs)
if err != nil {
log.Println("Unable to read file")
return
}
w.Header().Set("Content-Type", "application/force-download")
w.Header().Set("Content-Transfer-Encoding", "BINARY")
w.Header().Set("Content-Disposition", "attachment; filename=\""+file_name+"\"")
w.Header().Set("Content-length", strconv.Itoa(int(file_size)))
w.Header().Set("Pragma", "no-cache")
w.Write(bs)
}
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":80", nil)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment