我有这样的模板(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"}我尝试在模板中显示每一行的当前文件名,但是可以像这样将通道绑定到模板中吗?因为这样页面就不再加载了。
1 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
使用网络套接字。这里有些例子:
HTML/JavaScript:
<script>
var ws= new WebSocket("ws://yoursite.com");
ws.onmessage = function (event) {
console.log(event.data);
// $('#your-element').html(event.data);
}
</script>
去网络套接字:
func websocketSenderHandler(conn *websocket.Conn){
for {
msg := <- globalChannel
conn.WriteMessage(websocket.TextMessage, msg)
}
}
Go 中的更多 Websocket:golang.org/x/net/websocket
其他示例:https : //github.com/golang-samples/websocket
- 1 回答
- 0 关注
- 140 浏览
添加回答
举报
0/150
提交
取消