英文:
upload file with same format using beego
问题
文件上传已完成,但文件名不同于原始文件名。我在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) {
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, "无法创建文件,请检查写入权限")
return
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
}
fmt.Fprintf(w, "文件上传成功:")
fmt.Fprintf(w, header.Filename)
}
func main() {
http.HandleFunc("/", uploadHandler)
http.ListenAndServe(":8080", nil)
}
我能够实现文件上传,但文件名不同。现在我想要以与文件名相同的名称上传文件。
英文:
File Uploading is done but not with the same name as filename
i have tried this in html file
<!-- begin snippet: js hide: false -->
<!-- language: lang-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>
<!-- end snippet -->
and in beego/go receive.go
<!-- begin snippet: js hide: false -->
<!-- language: lang-html -->
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)
}
<!-- end snippet -->
i am able to achieve uploading file here with same format... but not with the same name..
now i want this file to be uploaded with same name as file name
答案1
得分: 7
你没有使用Beego控制器来处理上传。
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (this *MainController) GetFiles() {
this.TplNames = "aTemplateFile.html"
file, header, er := this.GetFile("file") // 这里的this是控制器,file是你表单字段的id
if file != nil {
// 获取文件名
fileName := header.Filename
// 保存到服务器
err := this.SaveToFile("file", somePathOnServer)
}
}
请注意,我只翻译了代码部分,其他内容不包括在内。
英文:
You are not using a Beego controller to handle the upload
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论