我有一个Web表单,它使用DevExpress ASPxUploadControl(v 8.3)让我们的用户将文档上传到我们的服务器。我正在进行一项增强功能,允许他们为文档选择上传文件夹。这些不是物理文件夹,而是逻辑文件夹。我已经在表单中添加了一个ASP:DropDownList,我从数据库中填充了用户已设置的文件夹的名称。如果用户选择文件夹,然后附件一切正常。但是,如果他们选择要上载的文件然后选择文件夹,则在选择文件后会自动上载文件,而不是等待他们单击“上载”链接。如果他们选择了错误的文档,则不允许他们更改。此外,使用从下拉列表中选择的旧(最后)值 - 它没有更新以反映新选择我怀疑服务器端事件和客户端事件之间存在冲突。以下是相关代码。如何阻止页面自动上传文档?
谢谢! 麦克
<td class="style6" valign="middle">
<asp:Panel ID="Panel3" colspan="2" runat="server" Height="83px" Width="125px"
Style="margin-top: 0px">
<dxuc:ASPxUploadControl ID="uplImage" runat="server"
ClientInstanceName="uploader" Font-Size="Medium" Height="84px"
OnFileUploadComplete="uplImage_FileUploadComplete" ShowUploadButton="True"
Size="50" style="margin-top: 0px; margin-right: 2px;">
<ValidationSettings AllowedContentTypes="image/jpeg,image/gif,image/pjpeg,application/pdf,application/rtf,application/msword,application/vnd.ms-excel,application/csv,text/plain,text/csv,text/richtext,text/rtf,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, audio/wav" MaxFileSize="4096000">
<ErrorStyle Font-Size="Smaller" />
</ValidationSettings>
<ClientSideEvents FileUploadComplete="function(s, e) {
if(e.isValid)
{
if(e.errorText != ''){
alert(e.errorText);
} else {
alert(e.callbackData);
}
}
}" />
<ValidationSettings AllowedContentTypes="image/jpeg, image/gif, image/pjpeg, application/pdf, application/rtf, application/msword, application/vnd.ms-excel, application/csv, text/plain, text/csv, text/richtext, text/rtf, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, audio/wav"
MaxFileSize="4096000">
<ErrorStyle Font-Size="Smaller" />
</ValidationSettings>
<ClientSideEvents FilesUploadComplete="function(s, e) {}" FileUploadComplete="function(s, e) {
if(e.isValid)
{
if(e.errorText != ''){
alert(e.errorText);
} else {
alert(e.callbackData);
}
}
}" FileUploadStart="function(s, e) {}" />
</dxuc:ASPxUploadControl>
</asp:Panel>
</td>
</tr>
<tr>
<td colspan="2" class="txtnote" align="center" style="height: 18px">
Allowed file types: jpeg, gif, pdf, rtf, doc, docx, txt, csv, xls, xlsx, wav
</td>
</tr>
<tr>
<td colspan="2" class="txtnote" align="center" style="height: 10px">
Maximum file size: 4Mb
</td>
</tr>
<tr>
<td></td>
<td>
<input id="chkPrivate" name="chkPrivate" value="1" type="checkbox"/>Private
(checking Private prevents Mgmt Co &Assn from viewing attachment via client
login.)</td></tr>
<tr class="aligncenter">
<th class="style5" align="center" colspan="2">
</th>
</tr>
<tr class="aligncenter">
<th class="style5" align="center" colspan="2">
<asp:Panel ID="Panel2" runat="server" Height="50px" Width="125px">
<%--<dxe:ASPxButton ID="Button1" runat="server" Font-Size="Medium" Text="Cancel">
<Paddings PaddingLeft="10px" PaddingRight="10px" />
</dxe:ASPxButton>--%>
<input type="button" id="btnReturn" class="frmBtnCommon" value="Back To Case" onclick="ReturnToCase();" />
</asp:Panel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim DAL As New DataAccessLayer
Dim dtFolders As New DataTable
GlobalVar.LoadData(Session("UserPKey"))
Session("CurrentPage") = "CaseAttchmt.aspx"
If Session("LoggedOn") = True And GlobalVar.ConnectString <> "" Then
CSSDefaultHTML = Session("CSS_Default")
DefaultIPHTML = Application("DefaultIP")
End If
SelKey = Request.QueryString("Case")
Header1.ConnectionStr = GlobalVar.ConnectString
Header1.HDLawFirm = GlobalVar.LawFirmDir
Header1.InsertHeader("Add File to a Case", 0, 0, SelKey, "width=100% align=center")
ClientKey = Session("ClientKey")
UploadDirectory = GlobalVar.LawFirmDir & "AttachFiles/Case" & Trim(Str(SelKey)) & "/"
If System.IO.Directory.Exists(GlobalVar.LawFirmDir & "AttachFiles/") = False Then
System.IO.Directory.CreateDirectory(GlobalVar.LawFirmDir & "AttachFiles")
End If
If System.IO.Directory.Exists(GlobalVar.LawFirmDir & "AttachFiles/Case" & Trim(Str(SelKey))) = False Then
System.IO.Directory.CreateDirectory(GlobalVar.LawFirmDir & "AttachFiles/Case" & Trim(Str(SelKey)))
End If
GlobalVar.SaveData(Session("UserPKey"))
dtFolders = DAL.GetClientFolders(Session("ClientKey"))
ddlFolders.DataSource = dtFolders
Dim lstFolders As ListItem
Dim i As Integer
Dim intValue As Integer
Dim strText As String
If Not Page.IsPostBack Then
ddlFolders.Items.Clear
intValue = 0
strText = "Attachments"
lstFolders = New ListItem(strText, intValue)
ddlFolders.Items.Add(lstFolders)
For i = 0 to dtFolders.Rows.Count - 1
intValue = dtFolders.Rows(i).Item("pKey")
strText = dtFolders.Rows(i).Item("FolderName")
lstFolders = New ListItem(strText, intValue)
ddlFolders.Items.Add(lstFolders)
Next
ddlFolders.SelectedIndex = 0
Session("FolderKey") = 0
End If
End Sub
Protected Function SavePostedFile() As String
Dim filename As String = ""
Dim tempFileName As String = ""
SelKey = Session("SelKey")
If Len(Trim(Session("ClientKey"))) = 4 then
UploadDirectory = "~/DATA/AR00" & Session("ClientKey") & "/AttachFiles/Case" & Trim(Str(SelKey)) & "/"
Else
UploadDirectory = "~/DATA/" & Trim(Session("LawFirm")) & "/AttachFiles/Case" & Trim(Str(SelKey)) & "/"
End If
filename = Trim(uplImage.FileName)
If filename <> "" Then
tempFileName = MapPath(UploadDirectory) & filename
uplImage.SaveAs(tempFileName)
End If
Return tempFileName
End Function
Protected Sub uplImage_FileUploadComplete(ByVal sender As Object, ByVal e As FileUploadCompleteEventArgs)
Dim FileExists As Boolean = False
UploadDirectory = "~/DATA/" & Trim(Session("LawFirm")) & "/AttachFiles/Case" & Trim(Str(Session("SelKey"))) & "/"
Dim FilePath As String = MapPath(UploadDirectory) & Trim(uplImage.FileName)
FileExists = CheckForFile(FilePath)
'urk : Added validation for checking filename length and filesize being uploaded 06/01/2010.
If (e.IsValid) Then
If (e.UploadedFile.FileName.Length > 100) Then
e.ErrorText = "The file name cannot be more than 100 characters long. Please shorten and retry."
ElseIf (e.UploadedFile.FileName.Contains("'") Or e.UploadedFile.FileName.Contains("&")) Then
e.ErrorText = "The file name cannot contain the apostrophe or ampersand characters."
ElseIf (e.UploadedFile.FileName.EndsWith(".aspx")) Or (e.UploadedFile.FileName.EndsWith(".ASPX")) Then
e.ErrorText = ".aspx files are not allowed for upload."
ElseIf FileExists = True Then
e.ErrorText = "A file with this name has already been uploaded. Please rename and try again."
Else
e.CallbackData = SavePostedFile()
If e.CallbackData <> "" Then
s_UpdateAttachData()
End If
e.CallbackData = "File: '" + e.UploadedFile.FileName + "' uploaded successfully."
End If
End If
End Sub
Private Sub s_UpdateAttachData()
Dim i_date As DateTime = Now.Date
Dim DAL As New DataAccessLayer
ClientKey = Session("ClientKey")
Dim pPrintBy As Integer = Session("UserPKey")
Dim PrintDate As DateTime = Now
Dim s As Boolean
Dim FullAttchPath As String
If Len(trim(ClientKey)) = 1 Then
FullAttchPath = "DATA/AR00000" + ClientKey.ToString + "/AttachFiles/Case" + SelKey.ToString + "/" + Trim(uplImage.FileName)
ElseIf Len(Trim(ClientKey)) = 4 then
FullAttchPath = "DATA/AR00" + ClientKey.ToString + "/AttachFiles/Case" + SelKey.ToString + "/" + Trim(uplImage.FileName)
Else
FullAttchPath = "DATA/AR0" + ClientKey.ToString + "/AttachFiles/Case" + SelKey.ToString + "/" + Trim(uplImage.FileName)
End If
If Request.Form("chkPrivate") <> "" Then
s = 1
Else
s = 0
End If
DAL.InsertCasesAttachPKey(ClientKey, SelKey, Trim(uplImage.FileName), Trim(uplImage.FileName), pPrintBy, PrintDate, s, FullAttchPath, Session("FolderKey"))
End Sub
Protected Function CheckForFile(ByVal Filename As String) As Boolean
If File.Exists(Filename) Then
Return True
Else
Return False
End If
End Function
Protected Sub ddlFolders_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlFolders.SelectedIndexChanged
Session("FolderKey") = ddlFolders.SelectedValue
End Sub