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.
覆盖了多恐怖啊,之前的文件就不存在了。 意味着可能重要信息丢失。
You can't avoid this. UDT uses the URL control of the DNN core, it is the way it handles it.
Stefan Cullmann
解决方案:
1.添加一个属性
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
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
上传事件修改
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
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!