Web服务器忽略savepath并保存到wwwroot,而localhost工作正常

时间:2014-04-10 20:08:07

标签: asp.net vb.net

当我尝试使用FileUpload1.SaveAs(savePath)时,它的工作非常奇怪。

使用localhost时,它会完美地使用savePath。我使用了各种路径和语法来确认这一点。但是,当我将其发布到我的网站并在那里使用上传功能时,它会将文件保存到wwwroot文件夹,而不是其他地方。

事实上,无论指定路径是什么,它总是保存到wwwroot,即使我告诉它。

Sub SaveFile(ByVal file As HttpPostedFile)

    ' Specify the path to save the uploaded file to.
    Dim savePath As String = Server.MapPath("\uploads\")

    ' Get the name of the file to upload.
    Dim fileName As String = FileUpload1.FileName

    ' Create the path and file name to check for duplicates.
    Dim pathToCheck As String = savePath + fileName

    ' Create a temporary file name to use for checking duplicates.
    Dim tempfileName As String

    ' Check to see if a file already exists with the
    ' same name as the file to upload.        
    If (System.IO.File.Exists(pathToCheck)) Then
        Dim counter As Integer = 2
        While (System.IO.File.Exists(pathToCheck))
            ' If a file with this name already exists,
            ' prefix the filename with a number.
            tempfileName = counter.ToString() + fileName
            pathToCheck = savePath + tempfileName
            counter = counter + 1
        End While

        fileName = tempfileName

        ' Notify the user that the file name was changed.
        UploadStatusLabel.Text = "A file with the same name already exists." + "<br />" + _
                                 "Your file was saved as " + fileName

    Else

        ' Notify the user that the file was saved successfully.
        UploadStatusLabel.Text = "Your file was uploaded successfully."

    End If

    ' Append the name of the file to upload to the path.
    savePath += fileName

    ' Call the SaveAs method to save the uploaded
    ' file to the specified directory.
    FileUpload1.SaveAs(savePath)

End Sub

一旦我解决了这个问题,我希望这条路径可以通过IIS创建一个虚拟目录,但由于这个问题,我无法测试它。

2 个答案:

答案 0 :(得分:0)

会不会像,

Dim savePath As String = Server.MapPath("~/uploads/")

只需将斜线方向更改为/

即可

答案 1 :(得分:0)

我发现上传过程的更改没有完全改变,除非我删除root文件并重新发布它们。在不删除初始文件的情况下发布更改不会修复上载过程。仍然有些神秘,但至少我找到了解决这个问题的难题。可能会在web.config或其他文件中进行更改,这会导致更改不会完全发生,除非重新启动。