dqxsuig64994 2019-03-13 10:18 采纳率: 0%
浏览 6
已采纳

文件未使用go上传到目录

Am trying to upload files to a directory using go code below. source

The Issue am having is that when I run the code, it prints files successfully uploaded but when I get to the directory, No file is uploaded there. Any solution will be appreciated. Thanks

package main

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

 func uploadHandler(w http.ResponseWriter, r *http.Request) {

    // the FormFile function takes in the POST input id file
    file, header, err := r.FormFile("file")

    if err != nil {
        fmt.Fprintln(w, err)
        return
    }

    defer file.Close()

    out, err := os.Create("/upload")
    if err != nil {
        fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
        return
    }

    defer out.Close()

    // write the content from POST to the file
    _, err = io.Copy(out, file)
    if err != nil {
        fmt.Fprintln(w, err)
    }

    fmt.Fprintf(w, "File uploaded successfully : ")
    fmt.Fprintf(w, header.Filename)
 }

 func main() {
    http.HandleFunc("/", uploadHandler)
    http.ListenAndServe(":8080", nil)
 }
  • 写回答

1条回答 默认 最新

  • doudaochu1699 2019-03-13 13:36
    关注

    Below is how I get my files uploaded to server. Source

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "net/http"
    )
    
    func uploadFile(w http.ResponseWriter, r *http.Request) {
        fmt.Println("File Upload Endpoint Hit")
    
        // Parse our multipart form, 10 << 20 specifies a maximum
        // upload of 10 MB files.
        r.ParseMultipartForm(10 << 20)
        // FormFile returns the first file for the given key `myFile`
        // it also returns the FileHeader so we can get the Filename,
        // the Header and the size of the file
        file, handler, err := r.FormFile("myFile")
        if err != nil {
            fmt.Println("Error Retrieving the File")
            fmt.Println(err)
            return
        }
        defer file.Close()
        fmt.Printf("Uploaded File: %+v
    ", handler.Filename)
        fmt.Printf("File Size: %+v
    ", handler.Size)
        fmt.Printf("MIME Header: %+v
    ", handler.Header)
    
        // Create a temporary file within our temp-images directory that follows
        // a particular naming pattern
        tempFile, err := ioutil.TempFile("temp-images", "upload-*.png")
        if err != nil {
            fmt.Println(err)
        }
        defer tempFile.Close()
    
        // read all of the contents of our uploaded file into a
        // byte array
        fileBytes, err := ioutil.ReadAll(file)
        if err != nil {
            fmt.Println(err)
        }
        // write this byte array to our temporary file
        tempFile.Write(fileBytes)
        // return that we have successfully uploaded our file!
        fmt.Fprintf(w, "Successfully Uploaded File
    ")
    }
    
    func setupRoutes() {
        http.HandleFunc("/upload", uploadFile)
        http.ListenAndServe(":8080", nil)
    }
    
    func main() {
        fmt.Println("Hello World")
        setupRoutes()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数