gpt4 book ai didi

asp.net - 回发后 FileUpload 控件的保留路径

转载 作者:行者123 更新时间:2023-12-04 05:27:07 26 4
gpt4 key购买 nike

我在 UpdatePanel 中有一个 FileUpload 控件和一个 DropDownlist 控件,当用户为 FileUpload 控件选择一个文件(尚未上传)时,同时用户从 DropDownList 控件中选择一个选项,这将导致回发!一旦页面回发,在 FileUpload 控件中选择的路径将消失。如何保留 FileUpload 控件中的路径?文件上传功能正在运行。我希望在回发期间可以保留 FileUpload 控件中的路径。

我已经尝试了下面的解决方案,但“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”在执行时会为真。
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

最佳答案

如果您将 FileUpload 从 UpdatePanel 中取出,它只会保留它的值。这样你仍然可以用 DropDownList 和它的 AutoPostBack 做所有事情,但是 ajax-postback 不会刷新 FileUpload 导致它变空。这样你就不再需要 postbacktriggers 了。

UpdatePanel仅在 DropDownList 附近并且回发的任何控件都必须更改 .如果这些控件不相邻,您可以使用多个 UpdatePanel,AutoPostBack 将刷新所有控件(默认行为,您甚至可以更改它)。

关于asp.net - 回发后 FileUpload 控件的保留路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3829508/

26 4 0