gpt4 book ai didi

file-upload - 使用beego上传相同格式的文件

转载 作者:IT王子 更新时间:2023-10-29 02:26:37 25 4
gpt4 key购买 nike

文件上传完成但与文件名不同我已经在 html 文件中试过了

<html>
<title>Go upload</title>
<body>

<form action="http://localhost:8080/receive" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

在 beego/go 中 receive.go

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("/home/vijay/Desktop/uploadfile")
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)
}

我能够在这里以相同的格式上传文件...但不能使用相同的名称..现在我希望这个文件以与文件名相同的名称上传

最佳答案

您没有使用 Beego Controller 来处理上传

package controllers

import (
"github.com/astaxie/beego"
)

type MainController struct {
beego.Controller
}

function (this *MainController) GetFiles() {
this.TplNames = "aTemplateFile.html"

file, header, er := this.GetFile("file") // where <<this>> is the controller and <<file>> the id of your form field
if file != nil {
// get the filename
fileName := header.Filename
// save to server
err := this.SaveToFile("file", somePathOnServer)
}
}

关于file-upload - 使用beego上传相同格式的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32310838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com