我是靠谱客的博主 自由时光,这篇文章主要介绍go.php文件,golang 上传文件到php,现在分享给大家,希望可以做个参考。

GO上传文件给PHP,第一篇文章里面少了个request.Header.Set(“Content-Type”, formcontenttype) 希望能够帮助别人

func upimgAction(imgurl string, url string) {

path := imgurl

extraParams := map[string]string{

"param1": "1",

"param2": "2",

"param3": "3",

}

request, err := newfileUploadRequest(url, extraParams, "Filedata", path)//Filedata文件名称

if err != nil {

log.Error(err)

}

//fmt.Println(request)

resp, err := Gclient.Do(request)

if err != nil {

log.Error(err)

} else {

body := &bytes.Buffer{}

_, err := body.ReadFrom(resp.Body)

if err != nil {

log.Error(err)

}

resp.Body.Close()

//fmt.Println(resp.StatusCode)

//fmt.Println(resp.Header)

fmt.Println(body)

}

}

func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {

file, err := os.Open(path)

if err != nil {

return nil, err

}

defer file.Close()

body := &bytes.Buffer{}

writer := multipart.NewWriter(body)

for key, val := range params {

_ = writer.WriteField(key, val)

}

formcontenttype := writer.FormDataContentType()

part, err := writer.CreateFormFile(paramName, filepath.Base(path))

if err != nil {

return nil, err

}

_, err = io.Copy(part, file)

err = writer.Close()

if err != nil {

return nil, err

}

//fmt.Println(body)

request, err := http.NewRequest("POST", uri, body)

request.Header.Set("Content-Type", formcontenttype)

return request, err

}

参考网站 http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/

http://stackoverflow.com/questions/20205796/golang-post-data-using-the-content-type-multipart-form-data

最后

以上就是自由时光最近收集整理的关于go.php文件,golang 上传文件到php的全部内容,更多相关go.php文件,golang内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(125)

评论列表共有 0 条评论

立即
投稿
返回
顶部