我发布到以下控制器操作:
<EmployeeAuthorize()>
<HttpPost()>
Function SendNewMessage(ByVal files1 As HttpPostedFileBase, ByVal files2 As HttpPostedFileBase) As JsonResult
Debug.Print("files1=" + files1.ToString)
Debug.Print("files2=" + files2.ToString)
Dim result = New Dictionary(Of String, String)
Dim fileName = Path.GetFileName(files1.FileName)
Dim filePath = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName)
files1.SaveAs(filePath)
Dim fileName2 = Path.GetFileName(files2.FileName)
Dim filePath2 = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName)
files2.SaveAs(filePath)
Return Json(result)
End Function
我在Debug.Print("files2=" + files2.ToString)
行上收到错误消息“NullReferenceException Object reference not set to an instance of an object.
”
这是我正在使用的视图:
@Using Html.BeginForm("SendNewMessage", "Message", FormMethod.Post, New With {.id = "sendForm", .enctype="multipart/form-data"})
@<div class="sendBox">
<h2 style="margin: 10px 0 0 0;">Attachments:</h2>
<label>Attachment 1: <input type="file" name="files1" id="file1" class="files"/></label>
<label>Attachment 2: <input type="file" name="files2" id="file2" class="files"/></label>
<label>Attachment 3: <input type="file" name="files3" id="file3" class="files"/></label>
<input type="submit" value="Send" />
</div>
End Using
第一个文件很好(files1
)。为什么我不能上传多个文件?
答案 0 :(得分:1)
在您的代码中,您使用相同的fileName
变量两次。对于您使用两次的filePath
变量,情况也是如此。所以替换:
Dim fileName2 = Path.GetFileName(files2.FileName)
Dim filePath2 = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName)
files2.SaveAs(filePath)
使用:
Dim fileName2 = Path.GetFileName(files2.FileName)
Dim filePath2 = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName2)
files2.SaveAs(filePath2)