왜 파일을 업로드하는~2,5MiB 또는 더 큰 원인은 다시 연결?

0

질문

우리를 구현하기 위해 노력하고 있습니다 이미지 업로드를 통해 POST 요청합니다. 우리는 또한 제한하려면 이미지를~1,0MiB. 그것은 잘 작동에 작은 이미지를,아무것도하지만~2,5MiB 또는 더 큰 원인 연결을 재설정합니다. 그것은 또한 것을 여러 요청을 보내 후 첫 번째 동일한 처리기입니다.

습니다.이동:

package main

import (
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", uploadHandler)
    http.ListenAndServe("localhost:8080", nil)
}

func uploadHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method == "POST" {
        postHandler(w, r)
        return
    } else {
        http.ServeFile(w, r, "index.html")
    }
}

func postHandler(w http.ResponseWriter, r *http.Request) {
    // Send an error if the request is larger than 1 MiB
    if r.ContentLength > 1<<20 {
        // if larger than ~2,5 MiB, this will print 2 or more times
        log.Println("File too large")
        // And this error will never arrive, instead a Connection reset
        http.Error(w, "response too large", http.StatusRequestEntityTooLarge)
        return
    }
    return
}

index.html:

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form method="POST" enctype="multipart/form-data">
      <input type="file" accept="image/*" name="profile-picture"><br>
      <button type="submit" >Upload</button>
  </form>
  </body>
</html>

출력 업로드할 때~2,4MiB 파일

$ go run main.go
2021/11/23 22:00:14 File too large

그것은 또한 요청"너무 큰에서"브라우저

출력 업로드할 때~2,5MiB 파일

$ go run main.go
2021/11/23 22:03:25 File too large
2021/11/23 22:03:25 File too large

브라우저 지금 보는 연결을 다시 설정

go http
2021-11-23 20:06:27
1

최고의 응답

3

클라이언트에 노력하고 데이터를 전송 서버입니다. 지 서버가 읽기 데이터를,그것은에 헤더와 연결을 닫. 클라이언트는 해석이라"연결을 재설정". 이것은 당신의 제어합니다.

대신 확인하는 헤더에 헤더를 수 있는 거짓말,사용 http.MaxBytesReader 을 읽을 실제 내용이지만,오는 경우 그것은 너무 큽니다.

const MAX_UPLOAD_SIZE = 1<<20

func postHandler(w http.ResponseWriter, r *http.Request) {
    // Wrap the body in a reader that will error at MAX_UPLOAD_SIZE
    r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)

    // Read the body as normal. Check for an error.
    if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
        // We're assuming it errored because the body is too large.
        // There are other reasons it could error, you'll have to
        // look at err to figure that out.
        log.Println("File too large")
        http.Error(w, "Your file is too powerful", http.StatusRequestEntityTooLarge)
        return
    }

    fmt.Fprintf(w, "Upload successful")
}

를 처리하는 방법 파일 업로드에서 이동 에 대한 더 세부 사항입니다.

2021-11-23 20:55:11

이 같은 작업을 확장하고 맞는 실제로 우리의 필요합니다. 그러나 그것은 여전히 연결을 다시 설정으로 큰 충분히 이미지 다음과 같 ,그래서 예를 들어,원하는 경우 인상을 MAX_UPLOAD_SIZE 20 << 20 어떤 이유에서 나는 할 수 없을 것이 아무것도 업로드의 크기입니다.
urist

또한 생각 r.ContentLength 로 사용할 수 있는 빠른 체크인하기 전에 모든 파일은 업로드 모두에서,내가 알고 있지만 그것이 될 수 있습니다. 내 생각 구현할 수 있는 이에 클라이언트 측의 것
urist

내 생각에 나는 이유에 연결 다시 설정에서 위의 의견,당신은 필요가 실제로 시작한 데이터를 사용하여(예를 들어과 r.FormFile),그렇지 않으면 그것은 그냥 정지고 반환하고 연결을 닫을 시작하면 얻는 화가에 의해 일정한 전송되는 데이터는 클라이언트.
urist

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................