我有这样的模板(upload.tmpl.html):
<html>
<body>
<div class="container">
<ul>
<li>current fileName : {{ .fileName}} </li>
</ul>
</body>
</html>
的处理程序uploadHandler.go与
func UploadHandler(c *gin.Context) {
file, header, err := c.Request.FormFile("file-upload")
if err != nil {
log.Fatal("Erreur dans la récupération de fichier")
}
//...
fileName := make(chan string)
go ReadCsvFile(bytes, fileName)
go func() {
for {
log.Info(<-fileName)
}
}()
c.HTML(http.StatusOK, "upload.tmpl.html", gin.H{
"fileName": <-fileName,
})
}
和这样的ReadCsvFile()方法:
func ReadCsvFile(bytesCSV []byte, fileName chan string) {
r := bytes.NewReader(bytesCSV)
reader := csv.NewReader(r)
reader.Comma = ';'
records, err := reader.ReadAll()
if err != nil {
fmt.Println("Error:", err)
return
}
db, _ := databaseApp.OpenDatabase()
defer db.Close()
for _, record := range records {
fileName <- record[0]
product := &em.Product{
Name: record[0],
//...
}
db.Create(product)
}
fileName <- "done"
}
我尝试在模板中显示每一行的当前文件名,但是可以像这样将通道绑定到模板中吗?因为这样页面就不再加载了。
守着星空守着你
相关分类