现在,我很高兴分享我的解决方案
func NewPostFile(url string, paramTexts map[string]interface{}, paramFile FileItem) ([]byte, error) {// if paramFiles ==nil {// return NewPost(url,paramTexts,header,transport)// }bodyBuf := &bytes.Buffer{}bodyWriter := multipart.NewWriter(bodyBuf)for k, v := range paramTexts { bodyWriter.WriteField(k, v.(string))}fileWriter, err := bodyWriter.CreateFormFile(paramFile.Key, paramFile.FileName)if err != nil { fmt.Println(err) //fmt.Println("Create form file error: ", error) return nil, err}fileWriter.Write(paramFile.Content)contentType := bodyWriter.FormDataContentType()bodyWriter.Close()fmt.Println(bodyBuf.String())resp, err := http.Post(url, contentType, bodyBuf)if err != nil { return nil, err}defer resp.Body.Close()fmt.Println(resp)if resp.StatusCode < 200 || resp.StatusCode >= 300 { b, _ := ioutil.ReadAll(resp.Body) return nil, fmt.Errorf("[%d %s]%s", resp.StatusCode, resp.Status, string(b))}respData, err := ioutil.ReadAll(resp.Body)if err != nil { return nil, err}fmt.Println(string(respData))return respData, nil}
type FileItem struct {Key string //image_contentFileName string //test.jpgContent []byte //[]byte}



