//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
- function uploadFile() {
- var fd = new FormData();
- var file=document.getElementById("files")
- for (var i=0;i<file.files.length;i++){
- fd.append("files",file.files[i])
- }
- var xhr = new XMLHttpRequest();
- xhr.upload.addEventListener("progress", uploadProgress, false);
- xhr.addEventListener("load", uploadComplete, false);
- xhr.addEventListener("error", uploadFailed, false);
- xhr.addEventListener("abort", uploadCanceled, false);
- xhr.open("POST", "test2.php");
- xhr.send(fd);
- }
- function uploadProgress(evt) {
- if (evt.lengthComputable) {
- var percentComplete = Math.round(evt.loaded * 100 / evt.total);
- document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
- }
- else {
- document.getElementById('progressNumber').innerHTML = 'unable to compute';
- }
- }
- function uploadComplete(evt) {
- /* This event is raised when the server send back a response */
- alert(evt.target.responseText);
- }
- function uploadFailed(evt) {
- alert("There was an error attempting to upload the file.");
- }
- function uploadCanceled(evt) {
- alert("The upload has been canceled by the user or the browser dropped the connection.");
- }
//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)
}
评论
发表评论