forked from albertba/Advertisement_Panel
Changed file structure to align with the recommendations given by https://github.com/golang-standards/project-layout
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("image") // "image" is the name of the file input
|
||||
if err != nil {
|
||||
http.Error(w, "Error retrieving file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
dst, err := os.Create("./uploads/" + header.Filename)
|
||||
if err != nil {
|
||||
http.Error(w, "Error creating file on server", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, file); err != nil {
|
||||
http.Error(w, "Error saving file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, "<script>location.href = '/admin/'</script>")
|
||||
fmt.Fprintf(w, "Image uploaded successfully: %s", header.Filename)
|
||||
}
|
||||
Reference in New Issue
Block a user