How can you upload files as a []byte in go?

huangapple go评论130阅读模式
英文:

How can you upload files as a []byte in go?

问题

我想使用golang进行POST请求并上传图片,但我不想传递文件路径,只想传递[]byte类型的数据。

以下是我找到的一篇文章,但不符合我的需求,因为它使用了os.Open函数:
https://stackoverflow.com/questions/20205796/golang-post-data-using-the-content-type-multipart-form-data

  1. func Upload(url, file string) (err error) {
  2. // 准备一个表单,将要提交到指定的URL。
  3. var b bytes.Buffer
  4. w := multipart.NewWriter(&b)
  5. // 添加图片文件
  6. f, err := os.Open(file)
  7. if err != nil {
  8. return
  9. }
  10. defer f.Close()
  11. fw, err := w.CreateFormFile("image", file)
  12. if err != nil {
  13. return
  14. }
  15. if _, err = io.Copy(fw, f); err != nil {
  16. return
  17. }
  18. // 添加其他字段
  19. if fw, err = w.CreateFormField("key"); err != nil {
  20. return
  21. }
  22. if _, err = fw.Write([]byte("KEY")); err != nil {
  23. return
  24. }
  25. // 不要忘记关闭multipart writer。
  26. // 如果不关闭,请求将缺少终止边界。
  27. w.Close()
  28. // 现在你有了一个表单,可以将其提交给处理程序。
  29. req, err := http.NewRequest("POST", url, &b)
  30. if err != nil {
  31. return
  32. }
  33. // 不要忘记设置Content-Type,它将包含边界信息。
  34. req.Header.Set("Content-Type", w.FormDataContentType())
  35. // 提交请求
  36. client := &http.Client{}
  37. res, err := client.Do(req)
  38. if err != nil {
  39. return
  40. }
  41. // 检查响应
  42. if res.StatusCode != http.StatusOK {
  43. err = fmt.Errorf("bad status: %s", res.Status)
  44. }
  45. return
  46. }

希望这可以帮到你!

英文:

I would like to use golang post request, upload pictures, but I do not want to pass filepath, just want to pass [] byte
The following article are not what I need because they are used os.Open
https://stackoverflow.com/questions/20205796/golang-post-data-using-the-content-type-multipart-form-data

  1. func Upload(url, file string) (err error) {
  2. // Prepare a form that you will submit to that URL.
  3. var b bytes.Buffer
  4. w := multipart.NewWriter(&b)
  5. // Add your image file
  6. f, err := os.Open(file)
  7. if err != nil {
  8. return
  9. }
  10. defer f.Close()
  11. fw, err := w.CreateFormFile("image", file)
  12. if err != nil {
  13. return
  14. }
  15. if _, err = io.Copy(fw, f); err != nil {
  16. return
  17. }
  18. // Add the other fields
  19. if fw, err = w.CreateFormField("key"); err != nil {
  20. return
  21. }
  22. if _, err = fw.Write([]byte("KEY")); err != nil {
  23. return
  24. }
  25. // Don't forget to close the multipart writer.
  26. // If you don't close it, your request will be missing the terminating boundary.
  27. w.Close()
  28. // Now that you have a form, you can submit it to your handler.
  29. req, err := http.NewRequest("POST", url, &b)
  30. if err != nil {
  31. return
  32. }
  33. // Don't forget to set the content type, this will contain the boundary.
  34. req.Header.Set("Content-Type", w.FormDataContentType())
  35. // Submit the request
  36. client := &http.Client{}
  37. res, err := client.Do(req)
  38. if err != nil {
  39. return
  40. }
  41. // Check the response
  42. if res.StatusCode != http.StatusOK {
  43. err = fmt.Errorf("bad status: %s", res.Status)
  44. }
  45. return
  46. }

答案1

得分: 1

由于您使用了以下代码:

  1. if _, err = io.Copy(fw, f); err != nil {
  2. return
  3. }

您可以对代码进行如下修改:

  1. 添加新的导入:"bytes"
  2. 将方法签名修改为 func Upload(url string, file []byte) (err error)
  3. 使用 io.Copy(fw, bytes.NewReader(f))
英文:

Since you use

  1. if _, err = io.Copy(fw, f); err != nil {
  2. return
  3. }

You may as well edit your code to:

  1. Add new import: "bytes"
  2. Change the method signature to func Upload(url string, file []byte) (err error)
  3. Use io.Copy(fw, bytes.NewReader(f))

huangapple
  • 本文由 发表于 2017年6月1日 10:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/44297376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定