posts - 404,  comments - 115,  views - 118万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

uploadOne.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="http://127.0.0.1:9090/uploadOne" method="post" enctype="multipart/form-data">
        文件:<input type="file" name="file" value="">
        <input type="submit" value="提交">
    </form>
</body>
</html>

uploadMore.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="http://127.0.0.1:9090/uploadMore" method="post" enctype="multipart/form-data">
        文件:<input type="file" name="file" value=""><br>
        文件:<input type="file" name="file" value=""><br>
        文件:<input type="file" name="file" value=""><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

main.go代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
 
import (
    "net/http"
    "os"
    "io"
    "html/template"
    "log"
)
 
func uploadOne(w http.ResponseWriter, r *http.Request) {
    //判断请求方式
    if r.Method == "POST" {
        //设置内存大小
        r.ParseMultipartForm(32 << 20);
        //获取上传的第一个文件
        file, header, err := r.FormFile("file");
        defer file.Close();
        if err != nil {
            log.Fatal(err);
        }
        //创建上传目录
        os.Mkdir("./upload", os.ModePerm);
        //创建上传文件
        cur, err := os.Create("./upload/" + header.Filename);
        defer cur.Close();
        if err != nil {
            log.Fatal(err);
        }
        //把上传文件数据拷贝到我们新建的文件
        io.Copy(cur, file);
    } else {
        //解析模板文件
        t, _ := template.ParseFiles("./uploadOne.html");
        //输出文件数据
        t.Execute(w, nil);
    }
}
 
func uploadMore(w http.ResponseWriter, r *http.Request) {
    if r.Method == "POST" {
        //设置内存大小
        r.ParseMultipartForm(32 << 20);
        //获取上传的文件组
        files := r.MultipartForm.File["file"];
        len := len(files);
        for i := 0; i < len; i++ {
            //打开上传文件
            file, err := files[i].Open();
            defer file.Close();
            if err != nil {
                log.Fatal(err);
            }
            //创建上传目录
            os.Mkdir("./upload", os.ModePerm);
            //创建上传文件
            cur, err := os.Create("./upload/" + files[i].Filename);
            defer cur.Close();
            if err != nil {
                log.Fatal(err);
            }
            io.Copy(cur, file);
        }
    } else {
        //解析模板文件
        t, _ := template.ParseFiles("./uploadMore.html");
        //输出文件数据
        t.Execute(w, nil);
    }
}
 
func main() {
    http.HandleFunc("/uploadMore", uploadMore);
    http.HandleFunc("/uploadOne", uploadOne);
    err := http.ListenAndServe(":9090", nil);
    if err != nil {
        log.Fatal(err);
    }
}

通过http://127.0.0.1:9090/uploadOne和http://127.0.0.1:9090/upladMore来测试文件上传。

posted on   怀素真  阅读(7822)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示