dnn URLControl 上传文件重名 如何不覆盖?

问题如题.  别人也有提过,LOOK:


I have a column in UDT, type "download".  So, when I upload file with the same name as existing file, it overwrites the old one.

How can I avoid this? I don't want users to change uploaded files in that way, because I need to see the old version of those files, like an archive.


覆盖了多恐怖啊,之前的文件就不存在了。  意味着可能重要信息丢失。

回复: http://local.dotnetnuke.com/Community/Forums/tabid/795/forumid/56/postid/271990/scope/posts/Default.aspx

 You can't avoid this. UDT uses the URL control of the DNN core, it is the way it handles it.


Stefan Cullmann

 

解决方案:

1.添加一个属性

ExpandedBlockStart.gif IsOverWrite属性
Public   Property  IsOverWrite()  As   Boolean
            
Get
                
If   Not  ViewState( " IsOverWrite " Is   Nothing   AndAlso   Trim (Convert.ToString(ViewState( " IsOverWrite " )))  <>   ""   Then
                    IsOverWrite 
=   CType (ViewState( " IsOverWrite " ),  Boolean )
                
Else
                    IsOverWrite 
=   False
                
End   If
            
End   Get
            
Set ( ByVal  Value  As   Boolean )
                ViewState(
" IsOverWrite " =  Value
            
End   Set
        
End Property


2.判断一下是否覆盖

 修改文件 Controls\URLControl.vb  中的事件cmdSave_Click

 

ExpandedBlockStart.gif 上传事件修改
Private   Sub  cmdSave_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  cmdSave.Click

            cmdUpload.Visible 
=   False

            
'  if no file is selected exit
             If  txtFile.PostedFile.FileName  =   ""   Then
                
Exit Sub
            
End   If

            
Dim  ParentFolderName  As   String
            
If  PortalSettings.ActiveTab.ParentId  =  PortalSettings.SuperTabId  Then
                ParentFolderName 
=  Common.Globals.HostMapPath
            
Else
                ParentFolderName 
=  PortalSettings.HomeDirectoryMapPath
            
End   If
            ParentFolderName 
+=  cboFolders.SelectedItem.Value

            
' change by xy 20100407   if the same fileName ,set a new fileName
             Dim  strExtension  As   String   =   Replace (Path.GetExtension(txtFile.PostedFile.FileName),  " . " "" )
            
Dim  rootPath  As   String   =  ParentFolderName.Replace( " / " " \ " )
            
Dim  sourceFileName  As   String   =  txtFile.PostedFile.FileName
            
Dim  strFileName  As   String   =  sourceFileName.Substring(sourceFileName.LastIndexOf( " \ " +   1 )
            sourceFileName 
=  rootPath  &  strFileName
            
If  FileFilter  <>   ""   And   InStr ( " , "   &  FileFilter.ToLower,  " , "   &  strExtension.ToLower)  =   0   Then
                
'  trying to upload a file not allowed for current filter
                lblMessage.Text  =   String .Format(Localization.GetString( " UploadError " Me .LocalResourceFile), FileFilter, strExtension)
            
Else
                
If  IsOverWrite  =   True   Then
                    lblMessage.Text 
=  FileSystemUtils.UploadFile(ParentFolderName.Replace( " / " " \ " ), txtFile.PostedFile,  False )
                
Else
                    
If  File.Exists(sourceFileName)  =   True   Then
                        
Dim  _now  As   String   =  System.DateTime.Now.ToString( " yyyyMMddHHmmss " , System.Globalization.CultureInfo.InvariantCulture)
                        
Dim  extension  As   String   =   " . "   +  strExtension
                        strFileName 
=  strFileName.Replace(extension,  "" &  _now  &  extension
                        sourceFileName 
=  rootPath  &  strFileName
                        lblMessage.Text 
=  FileSystemUtils.UploadFile(ParentFolderName.Replace( " / " " \ " ), txtFile.PostedFile, sourceFileName,  False )
                    
Else
                        lblMessage.Text 
=  FileSystemUtils.UploadFile(ParentFolderName.Replace( " / " " \ " ), txtFile.PostedFile,  False )
                    
End   If
                
End   If
            
End   If

            
If  lblMessage.Text  =   String .Empty  Then
                cboFiles.Visible 
=   True
                cmdUpload.Visible 
=  ShowUpLoad
                txtFile.Visible 
=   False
                cmdSave.Visible 
=   False
                cmdCancel.Visible 
=   False

                
Dim  Root  As   New  DirectoryInfo(ParentFolderName)
                cboFiles.Items.Clear()
                cboFiles.DataSource 
=  GetFileList( False )
                cboFiles.DataBind()

                
' modify by xy
                 ' Dim FileName As String = txtFile.PostedFile.FileName.Substring(txtFile.PostedFile.FileName.LastIndexOf("\") + 1)
                 If   Not  cboFiles.Items.FindByText(strFileName)  Is   Nothing   Then
                    cboFiles.Items.FindByText(strFileName).Selected 
=   True
                
End   If

                
If  cboFiles.SelectedIndex  >=   0   Then
                    ViewState(
" LastFileName " =  cboFiles.SelectedValue
                
Else
                    ViewState(
" LastFileName " =   ""
                
End   If
            
End   If

            _doRenderTypeControls 
=   False   ' Must not render on this postback
            _doRenderTypes  =   False
            _doChangeURL 
=   False
            _doReloadFolders 
=   False
            _doReloadFiles 
=   False
        
End Sub


 3.前台控件即可灵活配置是否覆盖

 <Portal:url id="ctlAttachment" runat="server" IsOverWrite="False" UrlType="F" />

 

 Well Done!

 

 

 

转载于:https://www.cnblogs.com/ajaxleoxu/archive/2010/04/07/1706816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值