package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
)
func main() {
bodyBuffer := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuffer)
// fieldname string : 在页面中file字段的name
// filename string : 上传的文件名
fileWriter, _ := bodyWriter.CreateFormFile("file1", "file.txt")
// 打开文件
file, _ := os.Open("file.txt")
defer func() { _ = file.Close() }()
// 将file复制到fileWriter中
_, _ = io.Copy(fileWriter, file)
// FormDataContentType返回具有此编写器边界的HTTP表单数据的内容类型。
contentType := bodyWriter.FormDataContentType()
log.Println(contentType)
defer func() { _ = bodyWriter.Close() }()
// http://127.0.0.1/ 自己的上传文件的地址
resp, _ := http.Post("http://127.0.0.1/", contentType, bodyBuffer)
defer func() { _ = resp.Body.Close() }()
respBody, _ := ioutil.ReadAll(resp.Body)
log.Println(resp.Status)
log.Println(string(respBody))
}
网站首页 > 开源技术 正文
猜你喜欢
- 2024-11-11 Golang Web编程,模板解析 if、else if、else语句
- 2024-11-11 Go语言Web编程,Request查询参数URL Query GET
- 2024-11-11 模玩资讯:千值练《机动机器人WeGo》第四弹 微型 MechatroWego 盒玩
- 2024-11-11 Go语言编程从入门到精通(字符串创建、拼接、转换、替换、查找)
- 2024-11-11 Golang Web编程,array数组、Slice切片、Map集合、Struct结构体
- 2024-11-11 Go语言编程从入门到精通,方法和接口
- 2024-07-25 Go语言编程从入门到精通,数据类型:布尔、数值、浮点、字符串
- 2024-07-25 Go语言编程从入门到精通(通道、缓冲通道、阻塞、控制、select)
- 2024-07-25 Go语言编程从入门到精通,流程控制之switch、for、defer
- 2024-07-25 Golang并发编程,4、无缓冲通道和有缓冲通道的区别
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)