D:\NorthwindForWeb\Northwind\CS\DotNet\ADO.Net\Northwind\WebControls\FileWUCBase.cs
/*
* This file was generated by ProCG version 2.0
*
* File name: Northwind\WebControls\FileWUCBase.cs
* Language: C# - ADO.Net
* Database: My Sql
*
* Copyright (c) 2002-2019 iGenXSoft.
* For more information visit http://www.igenxsoft.com
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Web.UI.SilverlightControls;
using System.IO;
using Northwind.General;
using Northwind.Data;
using Northwind.WebControls;
namespace Northwind.WebControls
{
[DefaultProperty("FileUrlInDBColumn")]
[ToolboxData("<{0}:FileWUC runat=server></{0}:FileWUC>")]
public class FileWUCBase : System.Web.UI.WebControls.CompositeControl, IPostBackDataHandler
{
private CStructData structData;
private string dataInstanceName = null; // was not set yet
private int dataInstanceIndex = -1; // was not set yet
private int fieldNum = -1; // was not set yet
public FileWUCBase()
{
}
public FileWUCBase(FileTypeEnum fileTypeEnum, FieldShowModeEnum showModeEnum)
{
FileType = fileTypeEnum;
ShowMode = showModeEnum;
}
public FileTypeEnum FileType
{
get
{
FileTypeEnum fileType;
if (ViewState["FileType"] == null)
{
fileType = FileTypeEnum.IMAGE;
return fileType;
}
fileType = (FileTypeEnum)ViewState["FileType"];
return (fileType);
}
set
{
ViewState["FileType"] = value;
}
}
public FieldShowModeEnum ShowMode
{
get
{
if (ViewState["ShowMode"] == null)
{
ShowMode = FieldShowModeEnum.SHOW_NORMAL;
return ShowMode;
}
FieldShowModeEnum showMode = (FieldShowModeEnum)ViewState["ShowMode"];
return (showMode);
}
set
{
ViewState["ShowMode"] = value;
}
}
public string DataInstanceName
{
get
{
if (dataInstanceName != null)
return dataInstanceName;
return (string)ViewState["DataInstanceName"];
}
set
{
ViewState["DataInstanceName"] = value;
dataInstanceName = value;
}
}
public int DataInstanceIndex
{
get
{
if (dataInstanceIndex != -1)
return dataInstanceIndex;
return (int)ViewState["DataInstanceIndex"];
}
set
{
ViewState["DataInstanceIndex"] = value;
dataInstanceIndex = value;
}
}
public CStructData StructData
{
get
{
if (structData != null)
return structData;
if (DataInstanceName == null)
{
// try to get the datainstance name for grid or wcc
Control parent = this.Parent;
while (parent != null)
{
if (parent is NorthwindGrid)
{
DataInstanceName = ((NorthwindGrid)parent).DataInstanceName;
break;
}
if (parent is NorthwindWCC)
{
DataInstanceName = ((NorthwindWCC)parent).DataInstanceName;
break;
}
if (parent is NorthwindWUC)
{
DataInstanceName = ((NorthwindWUC)parent).DataInstanceName;
break;
}
parent = parent.Parent;
}
}
if (DataInstanceName != null)
{
object dataObject = this.Page.Session[DataInstanceName];
if (dataObject != null)
{
if (dataObject is CStructData)
structData = (CStructData)dataObject;
else if (dataObject is IStructDataCollection)
{
IStructDataCollection structDataCollection = (IStructDataCollection)dataObject;
if (DataInstanceIndex != -1)
{
structData = structDataCollection.GetAtReturnsStructData(DataInstanceIndex);
}
else
{
throw new Exception("DataInstance is IStructDataCollection and DataInstanceIndex was not set");
}
}
else
{
throw new Exception("DataInstance must be IStructDataCollection or CStructData and it't type is " + dataObject.GetType().ToString());
}
}
else
{
throw new Exception("DataInstance is null");
}
}
else
{
// try to get the instance name form grid or from WCC
throw new Exception("DataInstanceName was not set, it is null, and this control was not inside Grid or WCC");
}
return structData;
}
set
{
structData = value;
}
}
public int FieldNum
{
get
{
if (fieldNum != -1)
return fieldNum;
if (ViewState["FieldNum"] != null)
fieldNum = (int)ViewState["FieldNum"];
return fieldNum;
}
set
{
ViewState["FieldNum"] = value;
fieldNum = value;
}
}
public bool HasPreExistingFiles
{
get
{
if (ViewState["HasPreExistingFiles"] != null)
{
bool hasPreExistingFiles = (bool)ViewState["HasPreExistingFiles"];
return hasPreExistingFiles;
}
return false;
}
set
{
ViewState["HasPreExistingFiles"] = value;
}
}
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
public override Control FindControl(string id)
{
EnsureChildControls();
return base.FindControl(id);
}
protected override void CreateChildControls()
{
switch (FileType)
{
case FileTypeEnum.IMAGE:
CreateImageChildControls();
break;
case FileTypeEnum.AUDIO:
CreateAudioChildControls();
break;
case FileTypeEnum.VIDEO:
CreateVideoChildControls();
break;
}
}
protected virtual void CreateImageChildControls()
{
Controls.Clear();
HtmlTableRow row;
HtmlTableCell cell;
HtmlGenericControl content;
HtmlTable table = new HtmlTable();
table.Border = 0;
// create <tr> - first row
row = new HtmlTableRow();
table.Controls.Add(row);
if (ShowMode == FieldShowModeEnum.SHOW_NORMAL)
{
// create <td>
cell = new HtmlTableCell();
TextBox userUrlTextBox = NorthwindWebEnv.CreateTextBox();
userUrlTextBox.ID = "UserUrlTextBox";
userUrlTextBox.CssClass = "hidden"; // Northwind.General.NorthwindGeneral.WebTextBoxClassName;
userUrlTextBox.TextChanged += new EventHandler(userUrlTextBox_TextChanged);
userUrlTextBox.AutoPostBack = true;
//userUrlTextBox.Width = 10;
userUrlTextBox.Visible = true;
cell.Controls.Add(userUrlTextBox);
Image image = new Image();
image.ID = "Image";
image.CssClass = Northwind.General.NorthwindGeneral.WebImageClassName;
cell.Controls.Add(image);
row.Controls.Add(cell);
// create <tr> - second row
row = new HtmlTableRow();
table.Controls.Add(row);
// create <td >
cell = new HtmlTableCell();
HtmlInputFile inputFile = new HtmlInputFile();
inputFile.Accept = "image/*";
inputFile.ID = "FileName";
cell.Controls.Add(inputFile);
Button uploadButton = new Button();
uploadButton.ID = "Upload";
uploadButton.Text = "Upload";
uploadButton.Height = 26; //vv
//uploadButton.CssClass = Northwind.General.NorthwindGeneral.WebButtonClassName;
uploadButton.Click += new EventHandler(uploadButton_Click);
cell.Controls.Add(uploadButton);
Literal literal = new Literal();
literal.Text = " ";
cell.Controls.Add(literal);
Button setPathButton = new Button();
setPathButton.CssClass = Northwind.General.NorthwindGeneral.WebButtonClassName;
setPathButton.ID = "SetFilePath";
setPathButton.Text = "Set File Path";
setPathButton.Click += new EventHandler(setFilePathButton_Click);
setPathButton.Visible = false;
cell.Controls.Add(setPathButton);
row.Controls.Add(cell);
// create <tr> - third row
row = new HtmlTableRow();
table.Controls.Add(row);
// create <td >
cell = new HtmlTableCell();
cell.ColSpan = 1;
Label message = new Label();
message.ID = "Message";
message.CssClass = Northwind.General.NorthwindGeneral.WebMessageClassName;
message.Visible = true;
cell.Controls.Add(message);
row.Controls.Add(cell);
}
else
{
// create <td>
cell = new HtmlTableCell();
Image image = new Image();
image.ID = "Image";
image.CssClass = Northwind.General.NorthwindGeneral.WebImageClassName;
cell.Controls.Add(image);
row.Controls.Add(cell);
}
this.Controls.Add(table);
}
protected virtual void CreateAudioChildControls()
{
Controls.Clear();
HtmlTableRow row;
HtmlTableCell cell;
HtmlGenericControl content;
HtmlTable table = new HtmlTable();
table.Border = 0;
// create <tr> - first row
row = new HtmlTableRow();
table.Controls.Add(row);
if (ShowMode == FieldShowModeEnum.SHOW_NORMAL)
{
// create <td>
cell = new HtmlTableCell();
TextBox userUrlTextBox = NorthwindWebEnv.CreateTextBox();
userUrlTextBox.ID = "UserUrlTextBox";
userUrlTextBox.CssClass = NorthwindGeneral.WebTextBoxClassName;
userUrlTextBox.TextChanged += new EventHandler(userUrlTextBox_TextChanged);
userUrlTextBox.AutoPostBack = true;
cell.Controls.Add(userUrlTextBox);
row.Controls.Add(cell);
// create <td>
cell = new HtmlTableCell();
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.CssClass = NorthwindGeneral.WebMediaPlayerClassName;
mediaPlayer.ID = "MediaPlayer";
mediaPlayer.Width = 300;
mediaPlayer.Height = 200;
cell.Controls.Add(mediaPlayer);
row.Controls.Add(cell);
/*
<asp:MediaPlayer ID="MediaPlayer2" runat="server" Height="100%" MediaSource="~/Resouces/Shiur 01.wmv"
Version="2.0" Width="100%" BorderWidth="2"
PlaceholderSource="~/Images/Advertise/hp5226.jpg">
</asp:MediaPlayer>
*/
// create <tr> - second row
row = new HtmlTableRow();
table.Controls.Add(row);
// create <td >
cell = new HtmlTableCell();
HtmlInputFile inputFile = new HtmlInputFile();
inputFile.ID = "FileName";
if (FileType == FileTypeEnum.AUDIO)
inputFile.Accept = "audio/*";
else
inputFile.Accept = "video/*";
cell.Controls.Add(inputFile);
row.Controls.Add(cell);
// create <td >
cell = new HtmlTableCell();
Button uploadButton = new Button();
uploadButton.CssClass = NorthwindGeneral.WebButtonClassName;
uploadButton.ID = "Upload";
uploadButton.Text = "Upload";
uploadButton.Click += new EventHandler(uploadButton_Click);
cell.Controls.Add(uploadButton);
Literal literal = new Literal();
literal.Text = " ";
cell.Controls.Add(literal);
Button setPathButton = new Button();
setPathButton.CssClass = NorthwindGeneral.WebButtonClassName;
setPathButton.ID = "SetFilePath";
setPathButton.Text = "Set File Path";
setPathButton.Click += new EventHandler(setFilePathButton_Click);
setPathButton.Visible = false;
cell.Controls.Add(setPathButton);
row.Controls.Add(cell);
// create <tr> - third row
row = new HtmlTableRow();
table.Controls.Add(row);
// create <td >
cell = new HtmlTableCell();
cell.ColSpan = 2;
Label message = new Label();
message.ID = "Message";
message.CssClass = NorthwindGeneral.WebMessageClassName;
message.Visible = false;
cell.Controls.Add(message);
row.Controls.Add(cell);
}
else
{
// create <td>
cell = new HtmlTableCell();
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.CssClass = NorthwindGeneral.WebMediaPlayerClassName;
mediaPlayer.ID = "MediaPlayer";
cell.Controls.Add(mediaPlayer);
row.Controls.Add(cell);
}
this.Controls.Add(table);
}
protected virtual void CreateVideoChildControls()
{
CreateAudioChildControls();
}
void userUrlTextBox_TextChanged(object sender, EventArgs e)
{
//if (UserUrlTextBox.Text.StartsWith("http:"))
FileUrlInDBColumn = UserUrlTextBox.Text;
}
void uploadButton_Click(object sender, EventArgs e)
{
// Get a reference to PostedFile object
HttpPostedFile myFile = this.FileName.PostedFile;
/*
if (this.Page.Request.Url.IsLoopback) // from local host - no need to upload
{
this.UserUrlTextBox.Text = myFile.FileName;
FileUrlInDBColumn = myFile.FileName;
StructData.SetFieldStringValue(FieldNum, FileUrlInDBColumn);
return;
}
*/
MessageLabel.Text = "";
// Get size of uploaded file
int nFileLen = myFile.ContentLength;
// Allocate a buffer for reading of the file
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream
//string strFilename = Path.GetFileName(myFile.FileName);
myFile.InputStream.Read(myData, 0, nFileLen);
if (myFile.FileName.Equals(""))
{
MessageLabel.Text = "Please choose file before 'Upload'";
return;
}
// check it is image files
string fileType;
FileInfo fileInfo = new FileInfo(myFile.FileName);
fileType = fileInfo.Extension.ToLower();
if (!fileType.Equals(".gif") && !fileType.Equals(".jpg") && !fileType.Equals(".png") && !fileType.Equals(".tif"))
{
MessageLabel.Text = "Unsupported file type! Please use gif,jpg,png or tif file types";
return;
}
string serverOutputFileName = GetServerOutputFileName(myFile.FileName);
WriteToFile(serverOutputFileName, ref myData);
switch (FileType)
{
case FileTypeEnum.IMAGE:
ImageControl.ImageUrl = FullFileUrl;
break;
case FileTypeEnum.AUDIO:
AudioControl.MediaSource = FullFileUrl;
break;
case FileTypeEnum.VIDEO:
VideoControl.MediaSource = FullFileUrl;
break;
}
StructData.SetFieldStringValue(FieldNum, FileUrlInDBColumn);
}
protected void setFilePathButton_Click(object sender, EventArgs e)
{
if (this.Page.Request.Url.IsLoopback) // from local host - no need to upload
{
this.UserUrlTextBox.Text = this.FileName.Value;
FileUrlInDBColumn = this.FileName.Value;
StructData.SetFieldStringValue(FieldNum, FileUrlInDBColumn);
return;
}
FileUrlInDBColumn = GetFileUrlInDBColum(this.UserUrlTextBox.Text);
StructData.SetFieldStringValue(FieldNum, FileUrlInDBColumn);
}
protected string GetFileUrlInDBColum(string textFromTextBox)
{
string fileUrlInDBColum;
if (textFromTextBox.Equals(""))
return "";
if (User.Type < (short)UserTypeEnum.ADMIN)
{
fileUrlInDBColum = User.UserName + @"\" + textFromTextBox;
fileUrlInDBColum = fileUrlInDBColum.Replace(@"\\", @"\\");
}
else // for admin or debug
{
fileUrlInDBColum = textFromTextBox;
}
return FileUrlInDBColumn;
}
protected string GetServerOutputFileName(string userFullFileName)
{
string serverOutputFileName;
string fileName = GetFileName(userFullFileName);
if (User.PrivateUploadDirectory != 0)
FileUrlInDBColumn = User.UserName + @"\" + fileName;
else
FileUrlInDBColumn = fileName;
serverOutputFileName = this.Context.Server.MapPath(NorthwindGeneral.GetRootWebUploadURL()) + FileUrlInDBColumn;
return serverOutputFileName;
}
private string GetUserServerUploadFilesDirectory()
{
string userServerUploadDirectory = null;
if (User.Type < (short)UserTypeEnum.ADMIN)
{
userServerUploadDirectory = User.ServerUploadFilesRootCalculated + @"\";
}
else // for admin or debug
{
switch (FileType)
{
case FileTypeEnum.IMAGE:
userServerUploadDirectory = this.Context.Server.MapPath(NorthwindGeneral.GetRootWebUploadImagesURL());
break;
case FileTypeEnum.AUDIO:
userServerUploadDirectory = this.Context.Server.MapPath(NorthwindGeneral.GetRootWebUploadAudioURL());
break;
case FileTypeEnum.VIDEO:
userServerUploadDirectory = this.Context.Server.MapPath(NorthwindGeneral.GetRootWebUploadVideoURL());
break;
}
}
return userServerUploadDirectory;
}
private string GetFileName(string userFullFileName)
{
string fileName;
string string1;
string string2;
int len = User.ClientUploadFilesRoot.Length;
if (userFullFileName.Length > len)
string1 = userFullFileName.ToLower().Substring(0, len);
else
string1 = "";
string2 = User.ClientUploadFilesRoot.ToLower();
if (string1.Equals(string2))
{
fileName = userFullFileName.Substring(User.ClientUploadFilesRoot.Length + 1);
}
else
{
int lastDirectorySign = userFullFileName.LastIndexOf(@"\");
if (lastDirectorySign == -1)
lastDirectorySign = userFullFileName.LastIndexOf(@"/");
fileName = userFullFileName.Substring(lastDirectorySign + 1);
}
return fileName;
}
public string FileUrlInDBColumn
{
get
{
String s = (String)ViewState["FileUrlInDBColumn"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["FileUrlInDBColumn"] = value;
}
}
protected string FullFileUrl
{
get
{
if (FileUrlInDBColumn.ToLower().StartsWith("http:"))
return FileUrlInDBColumn;
else
{
if (FileUrlInDBColumn.IndexOf(":") > 0) // it is a full path to the disk
{
return NorthwindGeneral.GetFileURLFromFullPath(FileType, FileUrlInDBColumn);
}
else
return NorthwindGeneral.GetRootWebUploadURL() + FileUrlInDBColumn;
}
}
}
// Writes file to current folder
private void WriteToFile(string strPath, ref byte[] Buffer)
{
DirectoryInfo directoryInfo = new DirectoryInfo(strPath);
FileInfo fileInfo = new FileInfo(strPath);
if (!directoryInfo.Exists)
{
if (User.CanCreateDirectories == 0)
{
MessageLabel.Text = "Directory not exist in server - Please contact support ServerFile: " + strPath;
MessageLabel.Visible = true;
return;
}
else
Directory.CreateDirectory(fileInfo.DirectoryName);
}
// Create a file
FileStream newFile = new FileStream(strPath, FileMode.Create);
// Write data to the file
newFile.Write(Buffer, 0, Buffer.Length);
// Close file
newFile.Close();
}
[Browsable(false)]
public string FileClientID
{
get
{
switch (FileType)
{
case FileTypeEnum.IMAGE:
return this.ImageControl.ClientID;
case FileTypeEnum.AUDIO:
return this.AudioControl.ClientID;
case FileTypeEnum.VIDEO:
return this.VideoControl.ClientID;
default:
NorthwindGeneral.ThrowInvalidFileTypeException(FileType);
return null;
}
}
}
[Browsable(false)]
public string FileNameClientID
{
get
{
return this.FileName.ClientID;
}
}
[Browsable(false)]
public CNorthwindUser User
{
get
{
NorthwindPage page = (NorthwindPage)this.Page;
return page.User;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
try
{
switch (FileType)
{
case FileTypeEnum.IMAGE:
ImageControl.ImageUrl = FullFileUrl;
break;
case FileTypeEnum.AUDIO:
AudioControl.MediaSource = FullFileUrl;
break;
case FileTypeEnum.VIDEO:
VideoControl.MediaSource = FullFileUrl;
break;
default:
NorthwindGeneral.ThrowInvalidFileTypeException(FileType);
break;
}
}
catch (Exception ex)
{
if (MessageLabel != null)
{
MessageLabel.Text = ex.Message;
MessageLabel.Visible = true;
}
}
if (ShowMode == FieldShowModeEnum.SHOW_NORMAL)
this.UserUrlTextBox.Text = UserFileUrl;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (MessageLabel != null)
MessageLabel.Text = "";
if (ShowMode == FieldShowModeEnum.SHOW_NORMAL && HasPreExistingFiles)
{
ImageControl.Attributes["onclick"] = "ChooseElementUsingTextFieldAndSize('/Pages/Product/ChoosePreexistPicture.html'," + UserUrlTextBox.ClientID + ",600,920);";
if (!this.Page.IsPostBack && MessageLabel != null)
MessageLabel.Text += "Click on Picture to choose from pre exist pictures";
}
}
protected string UserFileUrl
{
get
{
string userFileUrl;
if (User.Type < (short)UserTypeEnum.ADMIN)
{
if (FileUrlInDBColumn.ToLower().StartsWith("http:"))
userFileUrl = FileUrlInDBColumn;
else
{
if (FileUrlInDBColumn.Length > User.UserName.Length && FileUrlInDBColumn.Substring(0, User.UserName.Length).Equals(User.UserName))
{
userFileUrl = FileUrlInDBColumn.Substring(User.UserName.Length + 1);
}
else
{
userFileUrl = FileUrlInDBColumn;
}
}
}
else
{
userFileUrl = FileUrlInDBColumn;
}
return userFileUrl;
}
}
protected TextBox UserUrlTextBox
{
get
{
EnsureChildControls();
TextBox userUrlTextBox = (TextBox)this.FindControl("UserUrlTextBox");
System.Diagnostics.Debug.Assert(userUrlTextBox != null);
return userUrlTextBox;
}
}
public Image ImageControl
{
get
{
EnsureChildControls();
Image Image = (Image)this.FindControl("Image");
System.Diagnostics.Debug.Assert(Image != null);
return Image;
}
}
public MediaPlayer AudioControl
{
get
{
EnsureChildControls();
MediaPlayer mediaPlayer = (MediaPlayer)this.FindControl("MediaPlayer");
System.Diagnostics.Debug.Assert(mediaPlayer != null);
return mediaPlayer;
}
}
public MediaPlayer VideoControl
{
get
{
EnsureChildControls();
MediaPlayer mediaPlayer = (MediaPlayer)this.FindControl("MediaPlayer");
System.Diagnostics.Debug.Assert(mediaPlayer != null);
return mediaPlayer;
}
}
public HtmlInputFile FileName
{
get
{
EnsureChildControls();
HtmlInputFile fileName = (HtmlInputFile)this.FindControl("FileName");
System.Diagnostics.Debug.Assert(fileName != null);
return fileName;
}
}
public Label MessageLabel
{
get
{
EnsureChildControls();
Label message = (Label)this.FindControl("Message");
return message;
}
}
public bool LoadPostData(string postDataKey,
NameValueCollection postCollection)
{
string postedValue = postCollection[postDataKey];
this.FileUrlInDBColumn = postedValue;
return false;
}
public virtual void RaisePostDataChangedEvent()
{
}
}
}
// 26858 ProCG uses this line - don't edit it