ASP MVC Action中不能使用Path

时间:2012-10-17 16:53:00

标签: asp.net-mvc vb.net asp.net-mvc-3

我正在尝试使用Path(),但它下面有一条蓝线,并说“在声明它之前,不能引用局部变量(路径)。”我如何使用Path()?

Imports System.Globalization
Imports System.IO

Public Class MessageController
    Inherits System.Web.Mvc.Controller

    <EmployeeAuthorize()>
    <HttpPost()>
    Function SendReply(ByVal id As Integer, ByVal message As String, ByVal files As IEnumerable(Of HttpPostedFileBase)) As JsonResult


            ' upload files
            For Each i In files

                If (i.ContentLength > 0) Then

                    Dim fileName = path.GetFileName(i.FileName)
                    Dim path = path.Combine(Server.MapPath("~/App_Data/uploads"), fileName)
                    i.SaveAs(path)

                End If

            Next


    End Function

End Class

2 个答案:

答案 0 :(得分:2)

你可以尝试

Dim fileName As String
fileName = Path.GetFileName(i.FileName)
Dim path As String = Path.Combine(Server.MapPath("~/App_Data/uploads")

这里是参考文献

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx

答案 1 :(得分:0)

这里的问题是我使用Dim path这是不可能的,因为“path”是一个保留字。