/
ivan
/
ModMas
Обзор
Документация
Войти
/
ivan
/
ModMas
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ModuleMaster/usercontrols/advancedfileslist.ascx.vb
370 строк
13 KB
i.krivokon
Initial commit
13 мар 2026, 13:49
13 мар 2026, 13:49
00d9fb6
Код
Авторство
О чём код?
Imports System.Data.SqlClient Imports AjaxControlToolkit Imports System.IO Imports System.Data Imports sharedClasses.dbFunctions Partial Class UserControls_advancedFilesList Inherits System.Web.UI.UserControl Private bHasFiles As Boolean = False Protected bShowAsList As Boolean = False Private sf As New SqlFunctions Public ShowMode As String = "FILE" Private _style As String = "" Public Property Style As String Set(value As String) _style = value End Set Get Return _style End Get End Property Public Property SavePath As String Set(value As String) hfSavePath.Value = value End Set Get Return hfSavePath.Value End Get End Property Public Property StorageName As String Set(value As String) hfStorage.Value = value End Set Get Return hfStorage.Value End Get End Property Public Property GroupId As Integer Set(value As Integer) hfGroupId.Value = value End Set Get Return hfGroupId.Value End Get End Property Public Property GroupType As String Set(value As String) hfType.Value = value End Set Get Return hfType.Value End Get End Property Public Property ShowAsList As Boolean Set(value As Boolean) bShowAsList = value End Set Get Return bShowAsList End Get End Property Public Sub BuildFileList(b As Boolean) ShowAsList = b HideControls() BuildList(pnlMain) End Sub Public ReadOnly Property HasFiles As Boolean Get Try Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("SERKIntranetConnectionString").ConnectionString) Dim com As New SqlCommand("", con) com.CommandText = "SELECT count(*) FROM " & StorageName & " WHERE [deleted] = 'N' AND [type] = '" & hfType.Value & "' and [parentId] = " & hfGroupId.Value con.Open() Dim i As Integer = com.ExecuteScalar con.Close() Return i > 0 Catch ex As Exception Return False End Try End Get End Property Public Property TabIndex As Short Set(value As Short) ibAddFile.TabIndex = value End Set Get Return ibAddFile.TabIndex End Get End Property Public Event AddedAttachment(tableName As String, fieldName As String, rowid As String) Public Event DeletedAttachment(tableName As String, fieldName As String, rowid As String) Protected Sub btnYes_Click(sender As Object, e As System.EventArgs) Handles btnYes.Click pnlYesNo.Style("display") = "none" sf.NonQuery("UPDATE " & StorageName & " SET deleted = 'Y' where id = " & hfFileId.Value) RaiseEvent DeletedAttachment(StorageName, hfType.Value, hfFileId.Value) BuildFileList(ShowAsList) UpdatePanel1.Update() End Sub Protected Sub FileDeleting(sender As Object, e As EventArgs) hfFileId.Value = CType(sender, ImageButton).CommandArgument pnlYesNo.Style("display") = "block" upPopup.Update() End Sub Private Sub BuildList(p As Panel) p.Controls.Clear() Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("SERKIntranetConnectionString").ConnectionString) Dim com As New SqlCommand("", con) com.CommandText = "SELECT [id], [name] FROM " & StorageName & " WHERE [deleted] = 'N' AND [type] = '" & hfType.Value & "' and [parentId] = " & hfGroupId.Value Try con.Open() Dim reader As SqlDataReader = com.ExecuteReader If ShowAsList Then Select Case ShowMode Case "PHOTO" Dim tblPhotoes As New Table While reader.Read Dim img As New Image 'img.Attributes("onclick") = "window.open('" & Request.Url.GetLeftPart(UriPartial.Authority) & _ ' Request.ApplicationPath & "/handlers/getfile.ashx?id=" & _ ' reader.Item("id") & "&storage=" & StorageName & "')" img.CssClass = "thumbnail" img.ID = "img" & reader.Item("id") img.ImageUrl = Request.Url.GetLeftPart(UriPartial.Authority) & _ Request.ApplicationPath & "/handlers/getfile.ashx?id=" & reader.Item("id") & "&storage=" & StorageName applyStyle(img, _style) Dim r As New TableRow Dim c1 As New TableCell c1.Controls.Add(img) r.Cells.Add(c1) tblPhotoes.Rows.Add(r) End While p.Controls.Add(tblPhotoes) UpdatePanel1.Visible = True Case Else While reader.Read If ltrFilesList.Text <> "" And reader("name").ToString.Trim <> "" Then ltrFilesList.Text &= "<br />" 'ltrFilesList.Text &= " " End If ltrFilesList.Text &= "<a href='" & Request.Url.GetLeftPart(UriPartial.Authority) & _ Request.ApplicationPath & "/handlers/getfile.ashx?id=" & _ reader.Item("id") & "&storage=" & StorageName & "'>" & reader("name") & "</a>" End While UpdatePanel1.Visible = False End Select Else Dim tblFiles As New Table While reader.Read Dim cntrl As Control Dim b As New HyperLink Dim img As New Image Select Case ShowMode Case "PHOTO" img.ImageUrl = "~/handlers/getfile.ashx?id=" & reader.Item("id") & "&storage=" & StorageName 'img.Attributes("onclick") = "window.open('" & Request.Url.GetLeftPart(UriPartial.Authority) & _ ' Request.ApplicationPath & "/handlers/getfile.ashx?id=" & _ ' reader.Item("id") & "&storage=" & StorageName & "')" img.CssClass = "thumbnail" img.ID = "img" & reader.Item("id") applyStyle(img, _style) cntrl = img Case Else b.Text = reader.Item("name") b.NavigateUrl = "~/handlers/getfile.ashx?id=" & reader.Item("id") & "&storage=" & StorageName cntrl = b End Select Dim ib As New ImageButton ib.ImageUrl = "~/Images/Buttons/delete.png" ib.ID = "btnDeleteFile_" & hfType.Value & "_" & hfGroupId.Value & "_" & reader("id") ib.ClientIDMode = UI.ClientIDMode.Static ib.ToolTip = "Delete file" ib.CommandArgument = reader("id") AddHandler ib.Click, AddressOf FileDeleting Dim r As New TableRow Dim c1 As New TableCell Dim c2 As New TableCell c1.Controls.Add(cntrl) c2.Controls.Add(ib) r.Cells.Add(c1) r.Cells.Add(c2) tblFiles.Rows.Add(r) End While p.Controls.Add(tblFiles) If tblFiles.Rows.Count = 0 Then p.Style.Add("background-color", "red") Else p.Style.Add("background-color", "transparent") bHasFiles = True End If ''-------------------------------------------------------- Dim afu As New AsyncFileUpload afu.ID = "afu_" & hfType.Value & "_" & hfGroupId.Value AddHandler afu.UploadedComplete, AddressOf UploadComplete AddHandler afu.UploadedFileError, AddressOf UploadedFileError afu.ClientIDMode = UI.ClientIDMode.Static afu.Width = Unit.Pixel(220) 'afu.PersistedStoreType = AsyncFileUpload.PersistedStoreTypeEnum.Session afu.UploadingBackColor = Drawing.Color.SteelBlue afu.ErrorBackColor = Drawing.Color.Red pnlFileUpload.Controls.Clear() pnlFileUpload.Controls.Add(afu) ''-------------------------------------------------------- End If Catch ex As Exception Finally If con.State <> Data.ConnectionState.Closed Then con.Close() End If End Try End Sub Protected Sub UploadComplete(sender As Object, e As AsyncFileUploadEventArgs) 'Handles afu.UploadedComplete Log("Upload comlete : " & SavePath & e.FileName) Dim afu As AsyncFileUpload = DirectCast(sender, AsyncFileUpload) afu.SaveAs(SavePath & e.FileName) Log("File saved : " & SavePath & e.FileName) Session.Add("FileName", e.FileName) Session.Add("UploadedFile", e.FileName) End Sub Protected Sub UploadedFileError(sender As Object, e As AjaxControlToolkit.AsyncFileUploadEventArgs) 'Handles afu.UploadedFileError Log("Upload error : " & e.FileName) Dim err As String = e.FileName End Sub Private Sub applyStyle(ByRef img As Image, s As String) If s <> "" Then Dim style() As String = s.Split(";") If style.Count > 0 Then For Each st In style Dim stParams() As String = st.Split(":") If stParams.Count = 2 Then img.Style.Add(stParams(0), stParams(1)) End If Next End If End If End Sub Protected Sub ibAddFile_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ibAddFile.Click pnlAddFile.Style("display") = "block" upPopup.Update() End Sub Protected Sub ibCancel_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ibCancel.Click pnlAddFile.Style("display") = "none" upPopup.Update() End Sub Protected Sub ibSave_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ibSave.Click SaveFileToDb(Session("FileName"), hfGroupId.Value, hfType.Value) RaiseEvent AddedAttachment(StorageName, hfType.Value, hfGroupId.Value) Session("FileName") = Nothing pnlAddFile.Style("display") = "none" BuildFileList(ShowAsList) UpdatePanel1.Update() upPopup.Update() End Sub Private Sub Log(eventToLog As String) 'Dim fw As New FileStream(SavePath & "\log.txt", FileMode.Append) 'Dim sw As New StreamWriter(fw) 'sw.WriteLine(Context.Request.UserHostAddress.ToString & ":" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") & " : " & eventToLog) 'sw.Flush() 'sw.Close() 'fw.Close() 'fw.Dispose() End Sub Private Sub SaveFileToDb(ByVal sFileName As String, ByVal parentId As Integer, ByVal fileType As String) 'Saving files to DB Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("SERKIntranetConnectionString").ConnectionString) Dim com As New SqlCommand("", con) Dim f() As Byte Dim fs As FileStream Dim reader As BinaryReader com.CommandText = "INSERT INTO " & StorageName & " " & _ "([type], [parentId], [name], [doc]) VALUES " & _ "(@type, @parentid, @name, @doc)" com.Parameters.Clear() fs = New FileStream(SavePath & sFileName, FileMode.Open, FileAccess.Read) reader = New BinaryReader(fs) f = reader.ReadBytes(fs.Length) reader.Close() fs.Close() With com.Parameters .AddWithValue("type", fileType) .AddWithValue("parentId", parentId) .AddWithValue("name", sFileName) .AddWithValue("doc", f) End With Try con.Open() com.ExecuteNonQuery() con.Close() sf.NonQuery("UPDATE " & StorageName.Replace("UserModulesAttachments", "UserModules") & " SET [" & fileType & "]='Y' WHERE id=" & parentId) File.Delete(SavePath & sFileName) Return Catch ex As Exception If con.State <> ConnectionState.Closed Then con.Close() End If Return End Try End Sub Public Sub HideControls() If bShowAsList Then ibAddFile.Visible = False upPopup.Visible = False UpdatePanel1.Visible = False Else ibAddFile.Visible = True upPopup.Visible = True UpdatePanel1.Visible = True End If End Sub Protected Sub btnNo_Click(sender As Object, e As System.EventArgs) Handles btnNo.Click pnlYesNo.Style("display") = "none" upPopup.Update() End Sub End Class