Подтвердить что ты не робот

Как получить загруженный файл с помощью Golang net/http server?

Я играю с Mux и net/http. В последнее время я пытаюсь получить простой сервер с одной конечной точкой, чтобы принять загрузку файла.

Вот код, который у меня есть до сих пор:

server.go

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    router := mux.NewRouter()
    router.
        Path("/upload").
        Methods("POST").
        HandlerFunc(UploadCsv)
    fmt.Println("Starting")
    log.Fatal(http.ListenAndServe(":8080", router))
}

endpoint.go

package main

import (
    "fmt"
    "net/http"
)

func UploadFile(w http.ResponseWriter, r *http.Request) {
    err := r.ParseMultipartForm(5 * 1024 * 1024)
    if err != nil {
        panic(err)
    }

    fmt.Println(r.FormValue("fileupload"))
}

Я думаю, что я сузил проблему до фактического извлечения тела из запроса внутри UploadFile. Когда я запускаю эту команду cURL:

curl http://localhost:8080/upload -F "[email protected]" -vvv

Я получаю пустой ответ (как и ожидалось, я не печатаю на ResponseWriter), но я просто получаю новую (пустую) строку, напечатанную в приглашении, где я запускаю сервер, а не тело запроса.

Я отправляю файл как multipart (AFAIK, подразумеваемый с помощью -F а не -d в cURL), а подробный вывод cURL показывает 502 байта:

$ curl http://localhost:8080/upload -F "[email protected]" -vvv
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /upload HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.51.0
> Accept: */*
> Content-Length: 520
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------b578878d86779dc5
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Fri, 18 Nov 2016 19:01:50 GMT
< Content-Length: 0
< Content-Type: text/plain; charset=utf-8
< 
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact

Каким образом можно получать файлы, загруженные в виде данных с несколькими net/http используя net/http сервер в Go?

4b9b3361

Ответ 1

Вот краткий пример

func ReceiveFile(w http.ResponseWriter, r *http.Request) {
    var Buf bytes.Buffer
    // in your case file would be fileupload
    file, header, err := r.FormFile("file")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    name := strings.Split(header.Filename, ".")
    fmt.Printf("File name %s\n", name[0])
    // Copy the file data to my buffer
    io.Copy(&Buf, file)
    // do something with the contents...
    // I normally have a struct defined and unmarshal into a struct, but this will
    // work as an example
    contents := Buf.String()
    fmt.Println(contents)
    // I reset the buffer in case I want to use it again
    // reduces memory allocations in more intense projects
    Buf.Reset()
    // do something else
    // etc write header
    return
}

Ответ 2

Вы должны использовать FormFile вместо FormValue:

file, handler, err := r.FormFile("fileupload")
defer file.Close()

// copy example
f, err := os.OpenFile("./downloaded", os.O_WRONLY|os.O_CREATE, 0666)
defer f.Close()
io.Copy(f, file)

Ответ 3

Вот функция, которую я написал, чтобы помочь мне в загрузке моих файлов. Вы можете проверить полную версию здесь. Как загружать файлы в golang

package helpers

import (
    "io"
    "net/http"
    "os"
)

// This function returns the filename(to save in database) of the saved file
// or an error if it occurs
func FileUpload(r *http.Request) (string, error) {
    // ParseMultipartForm parses a request body as multipart/form-data
    r.ParseMultipartForm(32 << 20)

    file, handler, err := r.FormFile("file") // Retrieve the file from form data
    defer file.Close()                       // Close the file when we finish

    if err != nil {
        return "", err
    }

    // This is path which we want to store the file
    f, err := os.OpenFile("./images/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)

    if err != nil {
        return "", err
    }

    // Copy the file to the destination path
    io.Copy(f, file)

    return handler.Filename, nil
}