摘要:我想知道何时提交空白HTML文件字段,但是我的代码似乎只能捕获附加了文件的文件。有什么解决方法吗?
我有一个表单,允许用户提交包含一些有关它们的元数据的文件,如下所示:
<form method="post" action="/upload" enctype="multipart/form-data">
<label>
Paper abstract:
<input type="text" name="abstract" />
</label>
<label>
<div class="item">Upload paper in PDF format only</div>
<input type="file" name="file" />
</label>
<!-- the interface allows the html above to be duplicated and submit more papers, e.g., -->
<label>
Paper abstract:
<input type="text" name="abstract" />
</label>
<label>
<div class="item">Upload paper in PDF format only</div>
<input type="file" name="file" />
</label>
<button type="submit">Save</button>
</form>
服务器代码处理此类帖子:
func Upload(r *http.Request){
papers := len(r.Form["abstract"])//Check how many papers the user is submitting
r.ParseMultipartForm(config.MaxMemory)
for i := 0; i < papers; i++{
//Saves file and returns bytes -- implemented elsewhere
//This line will fail unless every file field has an attachment.
//I'd like to see blank file fields too.
path, fileSize := uploadFile(r.MultipartForm.File["file"][i])
paper := &Paper{//struct defined elsewhere
Abstract: r.Form["abstract"][i]
Path: path,
Size: size,
}
savePaper(paper) //implemented elsewhere
}
}
感谢任何提示。