我想允许上传文件.Go正在使用服务器端来处理请求.我想在他们尝试上传的文件太大时发送"文件太大"的响应.我想在上传整个文件(带宽)之前这样做.
我正在使用以下代码段,但它只是在客户端上传后才发送响应.它保存了5 kB文件.
const MaxFileSize = 5 * 1024
// This feels like a bad hack...
if r.ContentLength > MaxFileSize {
if flusher, ok := w.(http.Flusher); ok {
response := []byte("Request too large")
w.Header().Set("Connection", "close")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(response)))
w.WriteHeader(http.StatusExpectationFailed)
w.Write(response)
flusher.Flush()
}
conn, _, _ := w.(http.Hijacker).Hijack()
conn.Close()
return
}
r.Body = http.MaxBytesReader(w, r.Body, MaxFileSize)
err := r.ParseMultipartForm(1024)
if err != nil {
w.Write([]byte("File too large"));
return
}
file, header, err := r.FormFile("file")
if err != …
Run Code Online (Sandbox Code Playgroud) 我想解析一下time.Duration
.持续时间是"PT15M"
(字符串/字节),并希望将其转换为有效time.Duration
.
如果这是一time.Time
件事,我会用:
t, err := time.Parse(time.RFC3339Nano, "2013-06-05T14:10:43.678Z")
但这不存在(ParseDuration
只需要一个参数):
d, err := time.ParseDuration(time.RFC3339Nano, "PT15M")
我如何解析ISO 8601持续时间?