回发后保持FileUpload控制的路径 [英] Remain path of FileUpload control after postback
问题描述
我已经尝试了下面的解决方案,但是FileUpload1.HasFile将返回false给我。
如果Session(FileUpload1)是Nothing并且上传.HasFile然后
Session(FileUpload1)= Upload
lblPhotoUploadErr.Text = Upload.FileName
ElseIf Session(FileUpload1)IsNot Nothing AndAlso(Not Upload.HasFile)然后
Upload = DirectCast(Session(FileUpload1),FileUpload)
lblPhotoUploadErr.Text = Upload.FileName
ElseIf Upload.HasFile然后
Session(FileUpload1)= Upload
lblPhotoUploadErr.Text = Upload.FileName
End If
但是上传函数中的Upload.HasFile在执行时会为true。
Public Sub uploadPhoto()
Dim F ileOK 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
如果txtCandidateCode.Text.Trim<> Then
CandidateCode = txtCandidateCode.Text.Trim
End If
如果Upload.HasFile然后
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
$ b Upload.PostedFile.SaveAs((path1 + newFileName))
FileSaved = True
赶上例外
lblPhotoUploadErr.Text =(File could not be uploaded。+ ex.Message.ToString)
FileSaved = False
End Try
Else
lblPhotoUploadErr.Text =无法接受文件这种类型。
End If
如果FileSaved Then
pnlUpload.Visible = False
imgPhoto.ImageUrl =(〜/ images /+ newFileName)
hfPhotoUploadPath。 Value =(〜/ images /+ newFileName)
hfFileExtension.Value = extension
hfPhotoUploadFileName.Value = fileNameWithoutExt
End If
End Sub
如果您将FileUpload从UpdatePanel中取出。这样,你仍然可以使用DropDownList和AutoPostBack来做所有的事情,但是ajax-postback不会刷新FileUpload,导致它变空。这样你就不再需要postbacktriggers了。
$ b 将 UpdatePanel
放在 DropDownList
和任何控制回发必须改变。如果这些控件不相邻,则可以使用多个UpdatePanel,AutoPostBack将刷新所有这些控件(默认行为,甚至可以更改)。
I have a FileUpload control and a DropDownlist control in an UpdatePanel and when user select a file for the FileUpload control (no upload yet), in the meanwhile the user select an option from the DropDownList control which will cause a postback! Once the page postback, the path selected in the FileUpload control will gone. How can i remain the path in the FileUpload control? File uploading function was working. I hope can remain the path in the FileUpload control during postback.
I have tried the solution below but the "FileUpload1.HasFile" will return false to me.
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
but the "Upload.HasFile" in the uploading function below will true when it was executed.
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
The FileUpload will only keep it's value if you take it out of the UpdatePanel. That way you can still do everything with the DropDownList and its AutoPostBack but the ajax-postback won't refresh the FileUpload causing it to become empty. This way you don't need the postbacktriggers any more.
Put the UpdatePanel
only around the DropDownList
and any controls the postback has to change. If these controls are not next to each other you can use multiple UpdatePanels, the AutoPostBack will refresh all of them (default behavior, you can even change that).
这篇关于回发后保持FileUpload控制的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!