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

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

Passing file from JavaScript to Go server

问题

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

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

type Details {
    PhotographValue os.File `json:"photographvalue"`
    AdharValue      os.File `json:"adharvalue"`
    UserName        string  `json:"username"`
}

// 解码部分

var clientValues Details
decoder := json.NewDecoder(req.Body)
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.

type Details {

	PhotographValue  		os.File `json:"photographvalue"`
    AdharValue  		os.File `json:"adharvalue"`
    userName  		string `json:"username"`
}

//Decoding part

var clientValues Details
decoder := json.NewDecoder(req.Body)
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 使用。

func handleForm(w http.ResponseWriter, req *http.Request) {
    const maxAllowedSizeInBytes = 512 * 1024
    err := req.ParseMultipartForm(maxAllowedSizeInBytes)
    if err != nil {
        // 处理错误
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    photographValue, _, err := req.FormFile("photographvalue")
    if err != nil {
        // 处理错误
        w.WriteHeader(http.StatusBadRequest)
        return
    }
    adharValue, _, err := req.FormFile("adharvalue")
    if err != nil {
        // 处理错误
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    // photograph := struct{...} {}
    // json.NewDecoder(file).Decode(&photograph)
    // 或将其写入文件
    username := req.FormValue("username")
    ....
}

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.

func handleForm(w http.ResponseWriter, req *http.Request) {
	const maxAllowedSizeInBytes = 512 * 1024
	err := req.ParseMultipartForm(maxAllowedSizeInBytes)
	if err != nil {
		// handle error here
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	photographValue, _, err := req.FormFile("photographvalue")
	if err != nil {
		// handle error here
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	adharValue, _, err := req.FormFile("adharvalue")
	if err != nil {
		// handle error here
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// photograph := struct{...} {}
	// json.NewDecoder(file).Decode(&photograph)
	// or write it to file
	username := req.FormValue("username")
	....
}

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
  • file
  • go
  • javascript

Go模板:如何将array[]string打印到 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定