Imports System.IO Imports BVSoftware.BVC.Core Public Class products_products_edit_files Inherits BaseAdminPage Protected WithEvents FileUpload1 As MetaBuilders.WebControls.FileUpload Protected WithEvents MaxDownloads As System.Web.UI.WebControls.TextBox Protected WithEvents AvailableMinutes As System.Web.UI.WebControls.DropDownList Protected WithEvents Title As System.Web.UI.WebControls.TextBox Protected WithEvents dgFiles As System.Web.UI.WebControls.DataGrid Protected WithEvents inEditID As System.Web.UI.WebControls.TextBox Protected WithEvents AddFileButton As System.Web.UI.WebControls.ImageButton Protected WithEvents DeleteButton As MetaBuilders.WebControls.ConfirmedImageButton Protected WithEvents FirstFocus1 As MetaBuilders.WebControls.FirstFocus Protected WithEvents DefaultButtons1 As MetaBuilders.WebControls.DefaultButtons Protected WithEvents MetaDescriptionControl As BVSoftware.WebControls.MetaTag Protected WithEvents lblProductName As System.Web.UI.WebControls.Label Protected WithEvents msg As BVSoftware.WebControls.WebPageMessage Protected WithEvents ExistingFile As System.Web.UI.WebControls.TextBox Protected WithEvents rbUpload As System.Web.UI.WebControls.RadioButton Protected WithEvents rbLocal As System.Web.UI.WebControls.RadioButton Protected WithEvents MetaKeywordsControl As BVSoftware.WebControls.MetaTag #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles MyBase.Load BVC2004Store.CheckThisPage(Security.RolePermission.LoginToAdmin) BVC2004Store.CheckThisPage(Security.RolePermission.AccessProductArea) If Not Page.IsPostBack Then MetaKeywordsControl.Content = WebAppSettings.MetaKeywords MetaDescriptionControl.Content = WebAppSettings.MetaDescription End If If Not Page.IsPostBack() Then Dim sEditID As String sEditID = Request.Params("id") If Not sEditID = Nothing Then ViewState("EditID") = sEditID End If LoadFiles() SetProductName() End If End Sub Private Sub SetProductName() Dim p As New Catalog.Product p = CatalogServices.Products.GetProduct(Request.Params("ID")) If Not p Is Nothing Then Me.lblProductName.Text = p.ID & " " & p.ProductName End If p = Nothing End Sub Sub LoadFiles() dgFiles.DataSource = CatalogServices.Files.GetDownloadsForProduct(ViewState("EditID")) dgFiles.DataBind() End Sub Sub dgFiles_Delete(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dgFiles.DeleteCommand Dim deleteID As String = dgFiles.DataKeys(e.Item.ItemIndex) CatalogServices.Files.DeleteFileDownload(deleteID, ViewState("EditID")) LoadFiles() End Sub Private Sub AddFileButton_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles AddFileButton.Click msg.Clear() Dim sitePath As String = Request.PhysicalApplicationPath Dim filePath As String If Me.rbLocal.Checked = True Then Try If ExistingFile.Text.Trim.Length < 1 Then msg.ShowWarning("Enter a local file name first.") Else If File.Exists(sitePath & "\files\" & ExistingFile.Text.Trim) Then filePath = sitePath & "\files\" & ViewState("EditID") & "_" & ExistingFile.Text.Trim & ".config" File.Copy(sitePath & "\files\" & ExistingFile.Text.Trim, filePath) If File.Exists(filePath) = False Then msg.ShowWarning("The local file couldn't be renamed. Check file permissions.") Else ' Create Record linking to product If CatalogServices.Files.AddNewFileDownload(ViewState("EditID"), AvailableMinutes.SelectedItem.Value, MaxDownloads.Text.Trim, ExistingFile.Text.Trim, Title.Text.Trim) = True Then msg.ShowOK("File added!") LoadFiles() ' Clear out non-secured file. Try If File.Exists(filePath) Then File.Delete(sitePath & "\files\" & ExistingFile.Text.Trim) End If Catch ex As Exception End Try End If End If Else msg.ShowWarning("That local file couldn't be found. Please check your file name again.") End If End If Catch ex As Exception msg.ShowException(ex) End Try Else filePath = sitePath & "\files\" & ViewState("EditID") & "_" & FileUpload1.FileName & ".config" ' Upload the file If FileUpload1.FileName <> "" Then If File.Exists(filePath) = True Then msg.ShowWarning("That file already exists!") Else FileUpload1.PostedFile.SaveAs(filePath) 'lblSuccess.Text = "File Uploaded" ' Create Record linking to product If CatalogServices.Files.AddNewFileDownload(ViewState("EditID"), AvailableMinutes.SelectedItem.Value, MaxDownloads.Text.Trim, FileUpload1.FileName, Title.Text.Trim) = True Then msg.ShowOK("File added!") LoadFiles() Else msg.ShowError("The file couldn't be added.") If File.Exists(filePath) Then File.Delete(filePath) End If End If End If Else msg.ShowWarning("Please enter a file name first.") End If End If End Sub Sub dgFiles_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dgFiles.EditCommand Dim downloadID As String = dgFiles.DataKeys(e.Item.ItemIndex) Dim fileName As String = CatalogServices.Files.GetFileName(downloadID) Dim sitePath As String = Request.PhysicalApplicationPath Dim filePath As String = sitePath & "\files\" & ViewState("EditID") & "_" & fileName & ".config" Dim path As path Dim name = path.GetFileName(fileName) Dim ext = path.GetExtension(fileName) Dim fileSize As Double = 0 Dim type As String = "" type = ContentServices.GetMimeTypeForExtension(ext) If File.Exists(filePath) Then Dim f As New FileInfo(filePath) If Not f Is Nothing Then fileSize = f.Length() End If Response.Clear() Response.ClearContent() Response.ClearHeaders() 'Response.ContentType = "application/octet-stream" If type <> "" Then Response.ContentType = type End If 'If type = "application/octet-stream" Then Response.AppendHeader("content-disposition", "attachment; filename=" + name) 'End If If fileSize > 0 Then Response.AddHeader("Content-Length", fileSize.ToString()) End If Response.WriteFile(filePath) Response.Flush() Response.End() Else msg.ShowError("The file to download could not be found.") End If End Sub Sub dgFiles_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgFiles.ItemDataBound If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then Select Case e.Item.Cells(1).Text Case "0" e.Item.Cells(1).Text = "Unlimited" Case "15" e.Item.Cells(1).Text = "15 Minutes" Case "30" e.Item.Cells(1).Text = "30 Minutes" Case "60" e.Item.Cells(1).Text = "60 Minutes" Case "1440" e.Item.Cells(1).Text = "1 Day" Case "10080" e.Item.Cells(1).Text = "1 Week" Case "43200" e.Item.Cells(1).Text = "30 days" End Select End If End Sub End Class