代码拉取完成,页面将自动刷新
package main
import (
"io"
"net/http"
"os"
"time"
"html/template"
"io/ioutil"
"github.com/julienschmidt/httprouter"
"log"
)
func testPageHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
t, _:=template.ParseFiles("./videos/upload.html")
t.Execute(w, nil)
}
func streamHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
vid := p.ByName("vid-id")
vl := VIDEO_DIR + vid
video, err := os.Open(vl)
if err != nil {
sendErrorResponse(w, http.StatusInternalServerError, "error of open video: "+err.Error())
return
}
w.Header().Set("Content-Type", "video/mp4")
http.ServeContent(w, r, "", time.Now(), video)
defer video.Close()
}
func uploadHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)
if err:=r.ParseMultipartForm(MAX_UPLOAD_SIZE); err!=nil {
sendErrorResponse(w, http.StatusBadRequest, "File is too large")
return
}
//此处一定要这么写,否则读不出文件
file, _, err := r.FormFile("file")
if err!=nil {
sendErrorResponse(w, http.StatusInternalServerError, err.Error())
}
data, err:=ioutil.ReadAll(file)
if err!=nil {
log.Printf("Read file error: %v", err)
sendErrorResponse(w, http.StatusInternalServerError, err.Error())
}
fn:=p.ByName("vid-id")
err = ioutil.WriteFile(VIDEO_DIR + fn, data, 06666)
if err!=nil {
log.Printf("Write file error: %v", err)
sendErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
w.WriteHeader(http.StatusCreated)
io.WriteString(w, "Uploaded successfully.")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。