form单文件

  1. package main
  2. // 导入gin包
  3. import (
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "log"
  7. "net/http"
  8. )
  9. func main() {
  10. router := gin.Default()
  11. // 设置文件上传大小限制,默认是32m
  12. router.MaxMultipartMemory = 64 << 20 // 64 MiB
  13. router.POST("/upload", func(c *gin.Context) {
  14. // 获取上传文件,返回的是multipart.FileHeader对象,代表一个文件,里面包含了文件名之类的详细信息
  15. // file是表单字段名字
  16. file, _ := c.FormFile("file")
  17. // 打印上传的文件名
  18. log.Println(file.Filename)
  19. // 将上传的文件,保存到./data/1111.jpg 文件中
  20. c.SaveUploadedFile(file, "./data/1111.jpg")
  21. c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
  22. })
  23. router.Run(":8080")
  24. }

html代码

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Single file upload</title>
  6. </head>
  7. <body>
  8. <h1>上传文件演示</h1>
  9. <form action="/upload" method="post" enctype="multipart/form-data">
  10. 文件: <input type="file" name="file"><br><br>
  11. <input type="submit" value="上传文件">
  12. </form>
  13. </body>
  14. </html>

注意前端需要设置:enctype=”multipart/form-data”

form多文件

后端

  1. func DoFileUpload1(ctx *gin.Context) {
  2. form,_ :=ctx.MultipartForm()
  3. files := form.File["file_name"]
  4. for _,file := range files{
  5. fmt.Println(file.Filename)
  6. }
  7. ctx.String(http.StatusOK,"OK")
  8. }

前端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <form action="/upload1" method="post" enctype="multipart/form-data">
  9. <input type="file" name="file_name">
  10. <input type="file" name="file_name">
  11. <input type="submit">
  12. </form>
  13. </body>
  14. </html>

注意:前端上传的name相同才是多文件。

ajax单文件

后端

  1. func DoFileUpload1(ctx *gin.Context) {
  2. file,_:=ctx.FormFile("file")
  3. fmt.Print(file.Filename)
  4. ctx.String(http.StatusOK,"OK")
  5. }

前端

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="/static/js/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <form>
  10. <input type="file" name="file_name" id="file_id">
  11. <input type="button" id="button_id">
  12. </form>
  13. <script>
  14. var botton_id = document.getElementById("button_id")
  15. button_id.onclick = function(){
  16. var form_data = new FormData();
  17. form_data.append("file",$("#file_id")[0].files[0]);
  18. $.ajax({
  19. url:"/upload1",
  20. type:"POST",
  21. data:form_data,
  22. contentType: false,
  23. processData: false,
  24. success:function(data){
  25. alert(data["code"])
  26. },
  27. fail:function(data){}
  28. })
  29. }
  30. </script>
  31. </body>
  32. </html>

ajax多文件

后端

  1. func DoFileUpload1(ctx *gin.Context) {
  2. form,_ :=ctx.MultipartForm()
  3. files := form.File["file"]
  4. for _,file := range files{
  5. fmt.Println(file.Filename)
  6. }
  7. ctx.String(http.StatusOK,"OK")
  8. }

前端

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="/static/js/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <form>
  10. <input type="file" name="file_name" id="file_id">
  11. <input type="file" name="file_name" id="file_id">
  12. <input type="button" id="button_id">
  13. </form>
  14. <script>
  15. var botton_id = document.getElementById("button_id")
  16. button_id.onclick = function(){
  17. var form_data = new FormData();
  18. form_data.append("file",$("#file_id")[0].files[0]);
  19. form_data.append("file",$("#file_id")[1].files[0]);
  20. $.ajax({
  21. url:"/upload1",
  22. type:"POST",
  23. data:form_data,
  24. contentType: false,
  25. processData: false,
  26. success:function(data){
  27. alert(data["code"])
  28. },
  29. fail:function(data){}
  30. })
  31. }
  32. </script>
  33. </body>
  34. </html>