提问人:MCFH93 提问时间:8/4/2023 更新时间:8/4/2023 访问量:36
为上传文件调用 onuploadcompleted 后,无法调用函数 UploadDone()
Unable to call the function UploadDone() after onuploadcompleted is called for Upload File
问:
由于我对软件开发比较陌生,因此我得到了以下代码进行调试,因为当用户单击页面上的“浏览”时,它会弹出一个新窗口,上传文件后,文件不会自动填充在页面中的选择选项上,弹出窗口不会关闭。
以下是不同页面中的代码。请告知代码是否有任何问题。谢谢。
第 1 页
Public Shared Function NewUploadControl(ByVal ID As String, ByVal FilePath As String, ByRef Parent As Control) As AjaxControlToolkit.AsyncFileUpload
If Parent.Page Is Nothing Then
Err.Raise(513, , "Parent control is not added to the Page")
End If
Dim ps As New PageScript(Parent.Page, False)
ps.AddJavaScriptFile(PageScript.VirtualDir & "App_Files/_JavaScript/UIUploadFile.js", "UIUploadFile")
Dim file As New AjaxControlToolkit.AsyncFileUpload
Dim filetable As New HtmlControls.HtmlTable
Dim img As New Image
With filetable
.Width = "100%"
.CellPadding = 0
.CellSpacing = 0
.Rows.Add(New HtmlTableRow)
.Rows(0).Cells.Add(New HtmlTableCell)
.Rows(0).Cells.Add(New HtmlTableCell)
.Rows(0).Cells(0).Controls.Add(img)
.Rows(0).Cells(1).Controls.Add(file)
End With
''add loading image
img.ImageUrl = "~/_Images/loading.gif"
img.ID = "Loading_" & ID
img.ToolTip = "Uploading File..."
''draw file link
Dim FileName As String = ""
file.ID = ID
If Parent.Page.IsPostBack = False Then
ResetUploadControl(file)
End If
If FilePath <> "" Then
''add checkbox
Dim chk As New CheckBox
chk.ID = "Remove_" & ID
chk.AutoPostBack = False
chk.Text = "Remove"
Dim linktable As New HtmlControls.HtmlTable
With linktable
.Width = "100%"
.CellPadding = 0
.CellSpacing = 0
.Rows.Add(New HtmlTableRow)
.Rows(0).Cells.Add(New HtmlTableCell)
.Rows(0).Cells.Add(New HtmlTableCell)
.Rows(0).Cells(1).Controls.Add(chk)
.Rows(0).Cells(1).Align = "right"
.Rows(0).Cells(0).Controls.Add(NewFileLink(FilePath))
End With
Parent.Controls.Add(linktable)
End If
Parent.Controls.Add(filetable)
'Change 10-Added .UploaderStyle = AjaxControlToolkit.AsyncFileUploaderStyle.Traditional
'.UploaderStyle = AjaxControlToolkit.AsyncFileUpload.UploaderStyleEnum.Traditional
With file
.ID = ID
.CssClass = "TextBox"
.Height = Unit.Pixel(22)
.Width = Unit.Percentage(100)
.UploaderStyle = AjaxControlToolkit.AsyncFileUploaderStyle.Traditional
.ErrorBackColor = Drawing.Color.Gold
.UploadingBackColor = Drawing.Color.FromArgb(&H7EBF1F3)
.CompleteBackColor = Drawing.Color.White
.OnClientUploadError = "UIForm_UploadError"
.OnClientUploadComplete = "UIForm_UploadComplete"
.OnClientUploadStarted = "UIForm_UploadStarted"
.ThrobberID = img.ID
AddHandler file.UploadedComplete, AddressOf AsyncFileUpload_UploadedComplete
AddHandler file.UploadedFileError, AddressOf AsyncFileUpload_UploadedFileError
.Attributes.Item("Value") = CommonScript.FormatValue(FilePath, GetType(System.String).Name, CommonScript.FormatType.ValueFormat)
.Attributes.Item("OldValue") = CommonScript.FormatValue(FilePath, GetType(System.String).Name, CommonScript.FormatType.ValueFormat)
End With
Return file
End Function
Private Shared Sub AsyncFileUpload_UploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs)
Dim File As AjaxControlToolkit.AsyncFileUpload = sender
Dim Application As HttpApplicationState = HttpContext.Current.Application
Dim Session As HttpSessionState = HttpContext.Current.Session
Dim ErrorMessage As String = ""
Try
If (File.HasFile) Then
''check file type
Dim ValidFileType As Boolean = False
Dim ValidFileHeaderType As Boolean = False
If IsValidFileType(File.FileName, File.Attributes("AllowFileType")) Then
If IsValidFileType(File.FileName, PageScript.Session("AllowFileType")) Then
Select Case Path.GetExtension(File.FileName)
Case ".png"
If File.ContentType = "image/x-png" Or File.ContentType = "image/x-png" Then
ValidFileType = True
End If
Case ".jpeg", ".jpg"
If File.ContentType = "image/jpeg" Or File.ContentType = "image/jpg" Or File.ContentType = "image/pjpeg" Then
ValidFileType = True
End If
Case ".bmp"
If File.ContentType = "image/bmp" Then
ValidFileType = True
End If
Case ".gif"
If File.ContentType = "image/gif" Then
ValidFileType = True
End If
Case ".pdf"
Dim fileBytes As Byte() = ConverToBytes(File) 'Change 09
If (fileBytes.Take(7).SequenceEqual(PDF)) = False Then
ValidFileType = False
ValidFileHeaderType = False
Else
If File.ContentType = "application/pdf" Then
ValidFileType = True
End If
End If
Case ".doc"
Dim fileBytes As Byte() = ConverToBytes(File) 'Change 09
If (fileBytes.Take(8).SequenceEqual(DOC)) = False Then
ValidFileType = False
ValidFileHeaderType = False
Else
If File.ContentType = "application/msword" Then
ValidFileType = True
End If
End If
Case ".docx"
Dim fileBytes As Byte() = ConverToBytes(File) 'Change 09
If (fileBytes.Take(8).SequenceEqual(DOCX)) = False Then
ValidFileType = False
ValidFileHeaderType = False
Else
If File.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" Then
ValidFileType = True
End If
End If
Case ".ppt"
Dim fileBytes As Byte() = ConverToBytes(File)
If (fileBytes.Take(8).SequenceEqual(PPT)) = False Then
ValidFileType = False
ValidFileHeaderType = False
Else
If File.ContentType = "application/vnd.ms-powerpoint" Then
ValidFileType = True
End If
End If
Case ".pptx"
Dim fileBytes As Byte() = ConverToBytes(File)
If (fileBytes.Take(8).SequenceEqual(PPTX)) = False Then
ValidFileType = False
ValidFileHeaderType = False
Else
If File.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation" Then
ValidFileType = True
End If
End If
Case ".xls"
Dim fileBytes As Byte() = ConverToBytes(File)
If (fileBytes.Take(8).SequenceEqual(XLS)) = False Then
ValidFileType = False
ValidFileHeaderType = False
Else
If File.ContentType = "application/vnd.ms-excel" Then
ValidFileType = True
End If
End If
Case ".xlsx"
Dim fileBytes As Byte() = ConverToBytes(File)
If (fileBytes.Take(8).SequenceEqual(XLSX)) = False Then
ValidFileType = False
ValidFileHeaderType = False
Else
If File.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" Then
ValidFileType = True
End If
End If
Case ".zip"
If File.ContentType = "application/zip" Or File.ContentType = "application/x-zip-compressed" Then
ValidFileType = True
End If
Case ".txt"
If File.ContentType = "text/plain" Then
ValidFileType = True
End If
Case ".csv"
If File.ContentType = "text/csv" Or File.ContentType = "application/vnd.ms-excel" Or File.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" Then
ValidFileType = True
End If
Case Else
If ConfigurationManager.AppSettings("DenyFileType") = "" Then
ValidFileType = True
ElseIf IsValidFileType(File.FileName, ConfigurationManager.AppSettings("DenyFileType")) = False Then
ValidFileType = True
End If
If IsvalidFileExtensions(File.FileName) = True Then 'Change 09
ValidFileType = True
Else
ValidFileType = False
End If
End Select
End If
End If
If ValidFileType Then
Dim FileExt As String = ""
Dim OriginalFileName As String = File.FileName
OriginalFileName = SanitizeFileName(OriginalFileName)
If InStr(File.FileName, ".") > 0 Then FileExt = Mid(File.FileName, InStr(File.FileName, ".") + 1)
Dim TempFileName As String = Guid.NewGuid().ToString & "." & FileExt
Dim FilePath As String = FileFunctions.UploadAppTempFolderPath & TempFileName
Dim CopyTo As String = FileFunctions.UploadShareFolderPath(Session("CoID"), False) & TempFileName
File.SaveAs(FilePath) 'Save File to website > upload temp folder
ErrorMessage = FileFunctions.CopyFile(Session("UploadDoc_UserName"), Session("UploadDoc_Domain"), Session("UploadDoc_Password"), FilePath, CopyTo, True, Session("CoID"), OriginalFileName, True)
If ErrorMessage = "Successful" Then
FilePath = Replace(CopyTo, FileFunctions.UploadShareFolderPath(), "")
Session("UploadedFile_" & File.ID) = FilePath
Session("UploadedFileName_" & File.ID) = File.FileName
PageScript.JavaScript(File.Page, "UIForm_SetUploadError('')")
PageScript.JavaScript(File.Page, "UploadDone('" & CommonScript.StrEncode(FilePath, CommonScript.EncodeTypeEnum.JAVAW) & "','" & CommonScript.StrEncode(OriginalFileName, CommonScript.EncodeTypeEnum.JAVAW) & "')")
End If
Else
ErrorMessage = "Invalid File Type ! (" & Path.GetExtension(File.FileName) & ") " & File.ContentType
End If
End If
If ErrorMessage <> "" And ErrorMessage <> "Successful" Then
Session("UploadedFile_" & File.ID) = ""
Session("UploadedFileName_" & File.ID) = ""
PageScript.JavaScript(File.Page, "UIForm_SetUploadError('" & CommonScript.StrEncode(ErrorMessage, CommonScript.EncodeTypeEnum.JAVAW) & "')")
End If
Catch er As System.Exception
PageScript.JavaScript(File.Page, "UIForm_SetUploadError('" & CommonScript.StrEncode(er.Message, CommonScript.EncodeTypeEnum.JAVAW) & "')")
End Try
End Sub
第 2 页
<%@ Page Language="VB" %>
<%@ Import Namespace="iBase.CommonScript" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<script runat="server">
'** STANDARD Declarations **
Private ps As iBase.PageScript
Const PageTitle As String = "Upload File"
'** End of STANDARD Declarations **
Dim file As AjaxControlToolkit.AsyncFileUpload
Private Shared Login As iBase.Application.LoginScript
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
'** STANDARD Initialisations **
ps = New iBase.PageScript(Page)
'** End of STANDARD Initialisations **
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Cache.SetCacheability(HttpCacheability.NoCache)
'** STANDARD Initialisations **
Me.Header.Title = Application("AppTitle") & " - " & PageTitle
ps.PageLoad()
ps.ValidateForm = True
'** End of STANDARD Initialisations **
System.Diagnostics.Debugger.Break()
Dim validator As New Validator(ps)
file = FileFunctions.NewUploadControl("uploadfile", "", PlaceHolder1)
If IsPostBack = False Then
FileFunctions.ResetUploadControl(file)
End If
With file
.EnableViewState = False
.Attributes("onuploadcompleted") = "UploadDone"
End With
'Validator.Add(file, Component.Validator.ValidateType.Required, "")
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
<title></title>
</head>
<script language="JavaScript" type="text/javascript" src="<%=PageScript.VirtualDir%>App_Files/_JavaScript/UIForm.js"></script>
<script language="JavaScript" type="text/javascript">
function UploadDone(filepath, filename) {
UIForm_UploadDone(filepath, filename)
window.close()
}
</script>
<body topmargin="0" leftmargin="15">
<form id="form1" runat="server" enctype="multipart/form-data" method="post">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"
EnablePageMethods="false">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<div id="divForm" style="padding-top: 13px">
Click <b>Browse</b> to select the file.
<table width="100%" border="0" style="table-layout: fixed">
<colgroup>
<col align="left" width="90">
<tr height="6">
<td>
</td>
</tr>
<tr valign="top">
<td><span class="Label"><%= PageScript.Translate("Upload File")%></span>:
</td>
<td>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<i style="font-size:9pt"><%=PageScript.Translate("File Size Allowed")%> : <b><%=Application("MaxFileSizeMB")%>MB max.</b></td>
</td>
</tr>
<tr height="6">
<td>
</td>
</tr>
</col>
</colgroup>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
第 3 页
var UIForm_UploadFileID
function UIForm_UploadFile(elID) {
UIForm_UploadFileID = elID
var dialogattr = "dialogTop:0px;dialogLeft:0px;dialogWidth:500px;dialogHeight:195px;scroll:yes;status:no;"
formname = AppFolder + "App_Files/_Components/UploadFile.aspx"
winform = window.showModalDialog(formname, window, dialogattr, elID);
}
function UIForm_UploadDone(filepath, filename) {
if ((filepath != "") && (filename != "")) {
UIForm_SetUploadFile(UIForm_UploadFileID, filepath, filename)
}
}
function UIForm_SetUploadFile(elID, filepath, filename) {
if (elID) {
elID.options[elID.options.length] = new Option(filename, filepath)
elID.options[elID.options.length - 1].selected = true
}
}
第 4 页
<%Sub DrawFileObject(byval FieldName, byval FieldDesc, byval FieldValue, byval Folder)%>
<script language="JavaScript" src="<%=Application("VirtualDir")%>App_Files/_JavaScript/UIForm.js"></script><%'change 21%>
<table width="100%" cellpadding="0" cellspacing="0" style="table-layout:fixed">
<tr><td style="border:none">
<select style="width:100%" name="<%=FieldName%>" id="<%=FieldName%>" validate="0" title="<%=GetTabName()%><%=BiTranslate(FieldDesc)%>">
<option value></option>
<%if FieldValue<>"" then%>
<option value="<%=FieldValue%>" selected><%=GetFileName(FieldValue)%></option>
<%end if%>
</select>
</td>
<td style="border:none" width="150" align="center">
<a href="JavaScript:void(0)" style="padding-left:5px" onclick="ViewFile(this.parentElement.parentElement.children[0].children[0].value)"><%=Translate("View")%></a>
|<a href="JavaScript:void(0)" style="padding-left:5px" onclick="UploadFile(this.parentElement.parentElement.children[0].children[0])"><%=Translate("Browse")%>...</a>
</td></tr>
</table>
<%end sub%>
我发现,当单击“浏览文件”链接时,它会将HTMLElement对象传递到另一个页面,该页面将打开一个弹出窗口供用户上传文件。在另一页中,HTMLElement 对象存储在一个全局变量中,以便在上传完成时使用。但是,当上传完成时,全局变量将变为未定义,而不是存储 HTMLElement 对象。我还尝试将 HTMLElement 对象作为 URL 传递,但仍然失败了。
答: 暂无答案
评论