从表单导入CSV文件并使用GO解析结果。

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

Importing CSV file from form and parsing the results with GO

问题

所以我正在尝试导入一个从Excel转换而来的包含两列问题和答案的CSV文件。我只想解析文件并将其打印到控制台。当我选择我的文件sample.csv时,它只会退出并显示文件未找到的错误。任何帮助都将是很大的帮助。

HTML

   <form class="form-horizontal" method="post" action="/deck/{{ .deck.Id }}/bulkimport" >
           <div class="form-group">
          <div class="col-lg-10">
              <div class="alert alert-info">
                    <input type="file" name="file" style="visibility: hidden;" id="pdffile"/>
                    <label>选择文件</label>
                    <div class="input-append">
                    <input type="text" id="subfile" class="input-xlarge"/>
                    <a class="btn" onclick="$('#pdffile').click();">浏览</a>
                    </div>
                    <br/>
                  <!--   <button id="showHidden" class="btn btn-warning">显示/隐藏输入字段</button> -->
                    <input name="deck" value="{{.deck.Id}}" hidden> 
                    <button class="btn btn-primary" type="submit" name="submit">上传</button>
                    </div>
              </div>
            </div>
      </form>

GO FUNCTION

func BulkImortFlashCardsFormPage (req *http.Request, params martini.Params, c Flashcard,errors binding.Errors, r render.Render,session sessions.Session, db *DB) {
     id := params["id"]
     f, _, err := req.FormFile("file")
     if err != nil {
         // log.Fatal("找不到文件")
         session.AddFlash(FlashError {"global", "找不到文件", "", "error"})
         r.Redirect("/deck/" + id + "/cardlist")
         return
     }
	defer f.Close()
	reader := csv.NewReader(f)
	record, err := reader.ReadAll()
	if err != nil {
		fmt.Println("错误:", err)
		return
	}
	for _, line := range record {
		fmt.Println(line[1]) 
	}
	session.AddFlash(FlashError {"global", "卡片创建成功", "", "success"})
	r.Redirect("/deck/" + id + "/cardlist")
	return
    }
英文:

So I have am trying to import a csv file I converted from excel that has two columns a question and answer. I just want to parse the file and print it to console for now. When I select my file sample.csv it just does an exit 1 and says file not found. Any help would be a big help.

HTML

   <form class="form-horizontal" method="post" action="/deck/{{ .deck.Id }}/bulkimport" >
           <div class="form-group">
          <div class="col-lg-10">
              <div class="alert alert-info">
                    <input type="file" name="file" style="visibility: hidden;" id="pdffile"/>
                    <label>Choose A File</label>
                    <div class="input-append">
                    <input type="text" id="subfile" class="input-xlarge"/>
                    <a class="btn" onclick="$('#pdffile').click();">Browse</a>
                    </div>
                    <br/>
                  <!--   <button id="showHidden" class="btn btn-warning">Show/Hide Input Field</button> -->
                    <input name="deck" value="{{.deck.Id}}" hidden> 
                    <button class="btn btn-primary" type="submit" name="submit">Upload</button>
                    </div>
              </div>
            </div>
      </form>

GO FUNCTION

func BulkImortFlashCardsFormPage (req *http.Request, params martini.Params, c Flashcard,errors binding.Errors, r render.Render,session sessions.Session, db *DB) {
     id := params["id"]
     f, _, err := req.FormFile("file")
     if err != nil {
         // log.Fatal("Can't Find File ")
         session.AddFlash(FlashError {"global", "File not found", "", "error"})
         r.Redirect("/deck/" + id + "/cardlist")
         return
     }
	defer f.Close()
	reader := csv.NewReader(f)
	record, err := reader.ReadAll()
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	for _, line := range record {
		fmt.Println(line[1]) 
	}
	session.AddFlash(FlashError {"global", "Cards created succesfully", "", "success"})
	r.Redirect("/deck/" + id + "/cardlist")
	return
    }

答案1

得分: 1

错误在于您的HTML表单根本没有发送文件:为此,您需要在表单标签上声明enctype="multipart/form-data"属性。像这样:

<form method="post" action="/deck/{{ .deck.Id }}/bulkimport" enctype="multipart/form-data">
    […]
</form>
英文:

The error is that your HTML form isn't sending the file at all : for that you need to declare the enctype=&quot;multipart/form-data&quot; attribute on your form tag. Like that :

&lt;form method=&quot;post&quot; action=&quot;/deck/{{ .deck.Id }}/bulkimport&quot; enctype=&quot;multipart/form-data&quot;&gt;
    […]
&lt;/form&gt;

huangapple
  • 本文由 发表于 2014年6月6日 14:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/24075187.html
匿名

发表评论

匿名网友

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

确定