How to upload a file using golang and ajax

已查看 11,527 次
跳至第一个未读帖子

DEXTER

未读,
2012年2月10日 21:37:592012/2/10
收件人 golang-nuts
Hi
How do I upload files using ajax and golang ?

Thanks
Dexter

Paddy Foran

未读,
2012年2月10日 23:03:572012/2/10
收件人 DEXTER、golang-nuts
I think you're confused. AJAX is a Javascript technology. It doesn't really apply to Go. I think you probably want the http package, which will let you make HTTP requests.

Thanks,
Paddy Foran

GGGO

未读,
2012年2月10日 23:15:062012/2/10
收件人 golang-nuts
You need a web server in Go :
package main

import (
"fmt"
"net/http"
"log"
"io/ioutil"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
file, handler, err := req.FormFile("file")
if err != nil {
fmt.Println(err)
}
data, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
err = ioutil.WriteFile(handler.Filename, data, 0777)
if err != nil {
fmt.Println(err)
}
}

func main() {
http.HandleFunc("/upload", HelloServer)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

I don't have any example in ajax right now but a simple form in html
to post file :
<form enctype="multipart/form-data" action="http://localhost:8080/
upload" method="post">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>

With Ajax is the same process, you need a form in html and ajax to
post the form.

GGGO

DEXTER MCCONNELL

未读,
2012年2月10日 23:24:132012/2/10
收件人 Paddy Foran、golang-nuts
Hi Thanks for your reply,
I could not find any working code for http package file upload, can you point me to a url with sample code

Thank you
Dexter
--
...at the entrance gate of Heaven!

Kyle Finley

未读,
2012年2月10日 23:42:032012/2/10
收件人 golan...@googlegroups.com、Paddy Foran
jQuery File Upload has a Go backend build for Google App Engine:

Source:

Demo:

- Kyle

Daniel Theophanes

未读,
2012年2月11日 01:37:282012/2/11
收件人 golan...@googlegroups.com
The following uploads via a normal POST, not an AJAX post, but that change isn't hard to make.
It is a complete self contained example of how to upload a file.  I find it a useful utility.

in main.go lines 239 - 356

-Daniel

Sourabh

未读,
2016年7月26日 07:24:242016/7/26
收件人 golang-nuts
Nice code! I want to upload file from ios client to webserver. Any advice to extract image file information from client, so that it can be stored in server.
回复全部
回复作者
转发
0 个新帖子