登录
首页 >  Golang >  Go问答

文件未使用 go 上传到目录

来源:stackoverflow

时间:2024-04-07 13:09:23 364浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《文件未使用 go 上传到目录》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我正在尝试使用下面的代码将文件上传到目录。 来源

我遇到的问题是,当我运行代码时,它会打印文件已成功上传 但是当我到达该目录时,没有文件上传到那里。任何解决方案将不胜感激。谢谢

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)
 }

解决方案


下面是我如何将文件上传到服务器。 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\n", handler.Filename)
    fmt.Printf("File Size: %+v\n", handler.Size)
    fmt.Printf("MIME Header: %+v\n", 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\n")
}

func setupRoutes() {
    http.HandleFunc("/upload", uploadFile)
    http.ListenAndServe(":8080", nil)
}

func main() {
    fmt.Println("Hello World")
    setupRoutes()
}

终于介绍完啦!小伙伴们,这篇关于《文件未使用 go 上传到目录》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>