我有一个FileUpload控件和一个DropDownlist控件在UpdatePanel中,当用户选择FileUpload控件中的文件(还未上传),同时用户从DropDownList控件中选择一个选项,这将导致页面回发!一旦页面回发,FileUpload控件中选择的路径将消失。如何保留FileUpload控件中的路径?文件上传功能正常工作。我希望在回发期间可以保留FileUpload控件中的路径。
我已尝试以下解决方案,但是"FileUpload1.HasFile"会返回false。
但是在下面的上传函数中,“Upload.HasFile”将会在执行时为true。
我已尝试以下解决方案,但是"FileUpload1.HasFile"会返回false。
If Session("FileUpload1") Is Nothing AndAlso Upload.HasFile Then
Session("FileUpload1") = Upload
lblPhotoUploadErr.Text = Upload.FileName
ElseIf Session("FileUpload1") IsNot Nothing AndAlso (Not Upload.HasFile) Then
Upload = DirectCast(Session("FileUpload1"), FileUpload)
lblPhotoUploadErr.Text = Upload.FileName
ElseIf Upload.HasFile Then
Session("FileUpload1") = Upload
lblPhotoUploadErr.Text = Upload.FileName
End If
但是在下面的上传函数中,“Upload.HasFile”将会在执行时为true。
Public Sub uploadPhoto()
Dim FileOK As Boolean = False
Dim FileSaved As Boolean = False
Dim CandidateCode As String = Nothing
Dim newFileName As String = Nothing
Dim extension As String = Nothing
Dim fileNameWithoutExt As String = Nothing
If txtCandidateCode.Text.Trim <> "" Then
CandidateCode = txtCandidateCode.Text.Trim
End If
If Upload.HasFile Then
Dim FileExtension As String = Path.GetExtension(Upload.FileName).ToLower
Dim allowedExtensions() As String = {".png", ".jpeg", ".jpg", ".gif"}
Dim i As Integer = 0
Do While (i < allowedExtensions.Length)
If (FileExtension = allowedExtensions(i)) Then
FileOK = True
End If
i = (i + 1)
Loop
End If
If FileOK Then
Try
fileNameWithoutExt = Path.GetFileNameWithoutExtension(Upload.FileName)
extension = Path.GetExtension(Upload.FileName)
newFileName = fileNameWithoutExt + "_" + CandidateCode + extension
Upload.PostedFile.SaveAs((path1 + newFileName))
FileSaved = True
Catch ex As Exception
lblPhotoUploadErr.Text = ("File could not be uploaded." + ex.Message.ToString)
FileSaved = False
End Try
Else
lblPhotoUploadErr.Text = "Cannot accept files of this type."
End If
If FileSaved Then
pnlUpload.Visible = False
imgPhoto.ImageUrl = ("~/images/" + newFileName)
hfPhotoUploadPath.Value = ("~/images/" + newFileName)
hfFileExtension.Value = extension
hfPhotoUploadFileName.Value = fileNameWithoutExt
End If
End Sub