Html 检查表单post中是否缺少文件

Html 检查表单post中是否缺少文件,html,forms,go,Html,Forms,Go,小结:我想知道何时提交一个空白的HTML文件字段,但我的代码似乎只捕获那些附加了文件的字段。有什么解决办法吗 我有一个表单,允许用户提交带有元数据的文件,如下所示: <form method="post" action="/upload" enctype="multipart/form-data"> <label> Paper abstract: <input type="text" name="abstract" /> <

小结:我想知道何时提交一个空白的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

    }
}

论文摘要:
仅上传PDF格式的文件
论文摘要:
仅上传PDF格式的文件
拯救
服务器代码处理post的方式如下:

<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

    }
}
func上传(r*http.Request){
论文:=len(r.Form[“abstract”])//检查用户提交的论文数量
r、 ParseMultipartForm(config.MaxMemory)
对于i:=0;i
谢谢你的提示