du_1993 2014-08-10 05:11
浏览 44


I have a html page that has the follow code.

<form action="/image" method="post"
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">

I then have some Go code to get the file on the back end.

func uploaderHandler(w http.ResponseWriter, r *http.Request) {

   userInput := r.FormValue("file")

but anytime I try to use userInput from the go script, it returns nothing. Am I passing the variables wrong?

EDIT: I know how to upload things to golang that are text/passwords. I am having trouble uploading images to Go with the code.

EDIT 2: Read the Go guide a found the solution. See below.

  • 写回答

1条回答 默认 最新

  • duanpei8518 2014-08-10 05:36

    First you need to use req.FormFile not FormValue, then you will have to manually save the image to a file.

    Something like this:

    func HandleUpload(w http.ResponseWriter, req *http.Request) {
        in, header, err := req.FormFile("file")
        if err != nil {
            //handle error
        defer in.Close()
        //you probably want to make sure header.Filename is unique and 
        // use filepath.Join to put it somewhere else.
        out, err := os.OpenFile(header.Filename, os.O_WRONLY, 0644)
        if err != nil {
            //handle error
        defer out.Close()
        io.Copy(out, in)
        //do other stuff
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥30 计算机网络子网划分路由模拟操作
    • ¥15 MATLAB的画图问题
    • ¥15 c语言用fopen_s成功打开文件之后闪退
    • ¥20 用C++完成,并且运用数组
    • ¥30 求解电力系统潮流计算结果不收敛问题
    • ¥15 某易易盾点选data解析逆向
    • ¥15 系统崩溃,关于订单的处理
    • ¥15 datax-web连接hive为数据源时发生报错,如何解决?
    • ¥15 plink在进行gwas分析时总读取不到表型
    • ¥20 数据结构与c语言的实践内容