将文件从JavaScript传递给Go服务器

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

Passing file from JavaScript to Go server

问题

我正在尝试将一组表单值从客户端传递到服务器。表单值包括文件、用户名和其他详细信息。

在服务器端如何解码文件。我在服务器端使用Go语言。我已经创建了一个模型,以便在服务器调用期间解码传递的值。

  1. type Details {
  2. PhotographValue os.File `json:"photographvalue"`
  3. AdharValue os.File `json:"adharvalue"`
  4. UserName string `json:"username"`
  5. }

// 解码部分

  1. var clientValues Details
  2. decoder := json.NewDecoder(req.Body)
  3. err := decoder.Decode(&clientValues)

clientValues中,我得到了字符串数据,但文件数据为nil。我该如何实现这一点?

PS:我没有使用常规的文件上传方法,因为我还有其他详细信息,而且我在将其传递给服务器之前在JavaScript中对这些详细信息进行了操作。

英文:

I am trying to pass a set of FORM values from client to server. FORM values include files, user names and other details as well.

how do i decode the file in the server side. Here i Use go lang as my server.
i have created a model in my golang so that i can decode the value passed during server call.

  1. type Details {
  2. PhotographValue os.File `json:"photographvalue"`
  3. AdharValue os.File `json:"adharvalue"`
  4. userName string `json:"username"`
  5. }

//Decoding part

  1. var clientValues Details
  2. decoder := json.NewDecoder(req.Body)
  3. err := decoder.Decode(&clientValues)

In clientValues i get string data. but the file data is nil. How can i achieve this?

PS : i am not using the usual file upload method as i have other details too also, i am manipulating those details in javascript before passing on to server.

答案1

得分: 1

如果您将文件作为多部分表单的一部分传递,您应该将其解析为表单,并稍后将其作为单独的 JSON 使用。

  1. func handleForm(w http.ResponseWriter, req *http.Request) {
  2. const maxAllowedSizeInBytes = 512 * 1024
  3. err := req.ParseMultipartForm(maxAllowedSizeInBytes)
  4. if err != nil {
  5. // 处理错误
  6. w.WriteHeader(http.StatusBadRequest)
  7. return
  8. }
  9. photographValue, _, err := req.FormFile("photographvalue")
  10. if err != nil {
  11. // 处理错误
  12. w.WriteHeader(http.StatusBadRequest)
  13. return
  14. }
  15. adharValue, _, err := req.FormFile("adharvalue")
  16. if err != nil {
  17. // 处理错误
  18. w.WriteHeader(http.StatusBadRequest)
  19. return
  20. }
  21. // photograph := struct{...} {}
  22. // json.NewDecoder(file).Decode(&photograph)
  23. // 或将其写入文件
  24. username := req.FormValue("username")
  25. ....
  26. }

photographValueadharValue 都符合 io.Reader 接口,您可以使用它们来读取 JSON 文件或将其写入系统中的文件。

英文:

If you pass your files as a part of multipart form you should parse it as form and use later as a separate jsons.

  1. func handleForm(w http.ResponseWriter, req *http.Request) {
  2. const maxAllowedSizeInBytes = 512 * 1024
  3. err := req.ParseMultipartForm(maxAllowedSizeInBytes)
  4. if err != nil {
  5. // handle error here
  6. w.WriteHeader(http.StatusBadRequest)
  7. return
  8. }
  9. photographValue, _, err := req.FormFile("photographvalue")
  10. if err != nil {
  11. // handle error here
  12. w.WriteHeader(http.StatusBadRequest)
  13. return
  14. }
  15. adharValue, _, err := req.FormFile("adharvalue")
  16. if err != nil {
  17. // handle error here
  18. w.WriteHeader(http.StatusBadRequest)
  19. return
  20. }
  21. // photograph := struct{...} {}
  22. // json.NewDecoder(file).Decode(&photograph)
  23. // or write it to file
  24. username := req.FormValue("username")
  25. ....
  26. }

photographValue and adharValue conform io.Reader and you can use it to read your json file or write it to file in the system.

huangapple
  • 本文由 发表于 2022年4月29日 19:31:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/72057284.html
匿名

发表评论

匿名网友

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

确定