跳至主要内容

http form transfer/upload multiple files to golang server

//http  

POST http://localhost/upload HTTP/1.1
Host: localhost
Proxy-Connection: keep-alive
Content-Length: 468
Cache-Control: max-age=0
Origin: null
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary5hABI1kzSBUwtzsB
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
DNT: 1
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4

------WebKitFormBoundary5hABI1kzSBUwtzsB
Content-Disposition: form-data; name="files"; filename="a.txt"
Content-Type: text/plain

5522H-HY5KC-VL6QQ-IGCHV-YJP2H
------WebKitFormBoundary5hABI1kzSBUwtzsB
Content-Disposition: form-data; name="files"; filename="b.txt"
Content-Type: text/plain

asd

sadfa
dga
df
g
------WebKitFormBoundary5hABI1kzSBUwtzsB
Content-Disposition: form-data; name="submit"

提交
------WebKitFormBoundary5hABI1kzSBUwtzsB--

//HTML

<form action="http://localhost/upload" enctype="multipart/form-data" method="post">
<input type="file" name="files" id="files" multiple>
<input type="submit" name="submit" >
</form>

//XMLHttpRequest

  1.       function uploadFile() {  
  2.         var fd = new FormData();  
  3.         var file=document.getElementById("files")
  4.         for (var i=0;i<file.files.length;i++){
  5.              fd.append("files",file.files[i])
  6.         }
  1.         var xhr = new XMLHttpRequest();  
  2.         xhr.upload.addEventListener("progress", uploadProgress, false);  
  3.         xhr.addEventListener("load", uploadComplete, false);  
  4.         xhr.addEventListener("error", uploadFailed, false);  
  5.         xhr.addEventListener("abort", uploadCanceled, false);  
  6.         xhr.open("POST", "test2.php");  
  7.         xhr.send(fd);  
  8.       }  
  9.   
  10.       function uploadProgress(evt) {  
  11.         if (evt.lengthComputable) {  
  12.           var percentComplete = Math.round(evt.loaded * 100 / evt.total);  
  13.           document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';  
  14.         }  
  15.         else {  
  16.           document.getElementById('progressNumber').innerHTML = 'unable to compute';  
  17.         }  
  18.       }  
  19.   
  20.       function uploadComplete(evt) {  
  21.         /* This event is raised when the server send back a response */  
  22.         alert(evt.target.responseText);  
  23.       }  
  24.   
  25.       function uploadFailed(evt) {  
  26.         alert("There was an error attempting to upload the file.");  
  27.       }  
  28.   
  29.       function uploadCanceled(evt) {  
  30.         alert("The upload has been canceled by the user or the browser dropped the connection.");  
  31.       }  

//main.go

func uploadFile(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 30)
fhs := r.MultipartForm.File["myUploadFile"]
for _, v := range fhs {
file, err := v.Open()
if err != nil {
f.Println(err)
f.Fprintf(w, err.Error())
return
}
javahandler.OnFileReceived(v.Filename)
mf, err := os.OpenFile(str_storagePath+v.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
f.Println(err)
f.Fprintf(w, err.Error())
return
}
defer mf.Close()
io.Copy(mf, file)
    }

评论

此博客中的热门博文

onsen ui example splitter side menu swipe

<!DOCTYPE html> <html> <head> <title>TheyTube - Watch free video online</title> <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsenui.css"> <link rel="stylesheet" href="https://unpkg.com/onsenui/css/onsen-css-components.min.css">   <script src="https://unpkg.com/onsenui/js/onsenui.min.js"></script>   <script type="text/javascript">   ons.platform.select('android')   </script> </head> <body> <ons-splitter>   <ons-splitter-side id="menu" side="left" width="220px" collapse swipeable>     <ons-page>       <ons-list>         <ons-list-item onclick="fn.load('home.html')" tappable>           Home         </ons-list-item>         <ons-list-item onclick="fn.load('settings.html')" tappable>           Setti

go golang get disk usage free space remain info

package main import ( "fmt" "syscall" ) func main() { fmt.Println(DiskUsage("./")) } func DiskUsage(path string) uint64 { fs := syscall.Statfs_t{} err := syscall.Statfs(path, &fs) if err != nil { return 0 } return fs.Bfree * uint64(fs.Bsize) } //All space   = fs.Blocks * uint64(fs.Bsize) //Free space = fs.Bfree * uint64(fs.Bsize) //Used space= fs.All - disk.Free