0

我发布到以下控制器操作:

<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: &nbsp;<input type="file" name="files1" id="file1" class="files"/></label>
                <label>Attachment 2: &nbsp;<input type="file" name="files2" id="file2" class="files"/></label>
                <label>Attachment 3: &nbsp;<input type="file" name="files3" id="file3" class="files"/></label>

                <input type="submit" value="Send" />

        </div>

        End Using

第一个文件很好 ( files1)。为什么我不能上传多个文件?

4

1 回答 1

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)
于 2012-10-18T17:52:04.193 回答