using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
public partial class PageAll : System.Web.UI.Page
{
private string ConnectionString;
private static int dossierId = 1;
private static int extId = 9;
private static int parentId = 1;
private string thm;
private int sizedossier = int.Parse(ConfigurationManager.AppSettings["sizeDossier"]);
///
/// Pour l'execution du theme
///
///
///
protected void Page_PreInit(object sender, EventArgs e)
{
thm = ConfigurationManager.AppSettings["siteTheme"]; ;
if (thm != null)
{
Page.Theme = thm;
}
else
{
Session["themeSelected"] = thm;
Page.Theme = "Default";
}
}
public PageAll()
{
ConnectionString = ConfigurationManager.ConnectionStrings["PortailDocument"].ConnectionString;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (User.IsInRole("Publisher") || User.IsInRole("Administrateur"))
{
bUpload.Visible = true;
bCreerDossier.Visible = true;
lNomDossier.Visible = true;
tbNomDossier.Visible = true;
}
else
{
bUpload.Visible = false;
bCreerDossier.Visible = false;
lNomDossier.Visible = false;
tbNomDossier.Visible = false;
}
affichage(0);
gvSousDossier.PageSize = sizedossier;
}
catch
{
}
}
///
/// Action lors du clique sur le bouton Save
///
///
///
protected void bSaveFile_Click(object sender, EventArgs e)
{
int fid = Int32.Parse(DataList1.SelectedValue.ToString());
restaurer(fid,1);
}
///
/// Restaure le fichier / download le fichier
///
/// Id du fichier
/// Suivant le bouton qui a été poussé 1 Save / 0 Lecture du fichier sur le serveur
public void restaurer(int fId, int test)
{
string directorytest = Server.MapPath("~/Temps/");
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = myConnection.CreateCommand();
myConnection.Open();
myCommand.CommandText = "SELECT données FROM fichiers WHERE fichiersId = @fichiersId";
myCommand.CommandType = CommandType.Text;
SqlParameter fichiersId = new SqlParameter();
fichiersId.ParameterName = "@fichiersId";
fichiersId.SqlDbType = SqlDbType.Int;
fichiersId.Value = fId;
myCommand.Parameters.Add(fichiersId);
Byte[] fileDataBytes = (Byte[])myCommand.ExecuteScalar();
String fileData = Encoding.ASCII.GetString(fileDataBytes);
SqlCommand sqlcom2 = myConnection.CreateCommand();
sqlcom2.CommandText = "SELECT nom FROM fichiers WHERE fichiersId=" + fId;
SqlDataReader sqldata = sqlcom2.ExecuteReader();
while (sqldata.Read())
{
string namefichier = sqldata.GetString(0);
FileStream fs = new FileStream(directorytest + namefichier, FileMode.Create);
byte[] data = fileDataBytes;
fs.Write(data, 0, System.Convert.ToInt32(data.Length));
fs.Seek(0, SeekOrigin.Begin);
fs.Close();
//File.Delete(directorytest + namefichier);
Byte[] fichierUpload = fileDataBytes;
if (test == 1)
{
Response.ContentType = "text/txt";
Response.AddHeader("content-disposition", "attachment; filename=" + namefichier);
Response.BinaryWrite(fichierUpload);
File.Delete(directorytest + namefichier);
}
else
{
if (test == 0)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + directorytest + namefichier;
Process processus = new Process();
processus.StartInfo.FileName = path;
processus.Start();
processus.Close();
}
}
}
myConnection.Close();
}
///
/// Création de dossier
///
///
///
protected void bCreerDossier_Click(object sender, EventArgs e)
{
if (tbNomDossier.Text != "")
{
int dossierId = int.Parse(DropDownDossier.SelectedValue.ToString());
SqlConnection myConnection = new SqlConnection(ConnectionString);
string path = "";
try
{
String query = "SELECT path FROM fichiers WHERE fichiersId = @fId";
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlParameter fId = new SqlParameter();
fId.ParameterName = "@fId";
fId.SqlDbType = SqlDbType.Int;
fId.Value = dossierId;
myCommand.Parameters.Add(fId);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.HasRows)
{
myReader.Read();
}
path = myReader.GetValue(0).ToString();
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
myConnection.Close();
}
string nomDossier = tbNomDossier.Text;
saveFileInDatabase(path, nomDossier, null);
}
}
///
/// Save Le dossier en db
///
/// Chemin du dossier Parent
/// Nom du dossier
/// Null
public void saveFileInDatabase(string cheminParent,string nomdossier, Byte[] Data)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = myConnection.CreateCommand();
myConnection.Open();
myCommand.CommandText = "SauverFichier";
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter nom = new SqlParameter();
nom.ParameterName = "@nom";
nom.SqlDbType = SqlDbType.VarChar;
nom.Value = nomdossier;
myCommand.Parameters.Add(nom);
SqlParameter path = new SqlParameter();
path.ParameterName = "@path";
path.SqlDbType = SqlDbType.VarChar;
path.Value = cheminParent + nomdossier + "\\";
myCommand.Parameters.Add(path);
SqlParameter dateFichier = new SqlParameter();
dateFichier.ParameterName = "@dateFichier";
dateFichier.SqlDbType = SqlDbType.DateTime;
dateFichier.Value = DateTime.Now;
myCommand.Parameters.Add(dateFichier);
SqlParameter données = new SqlParameter();
données.ParameterName = "@données";
données.IsNullable = true;
données.SqlDbType = SqlDbType.Binary;
données.Value = DBNull.Value;
myCommand.Parameters.Add(données);
SqlParameter type = new SqlParameter();
type.ParameterName = "@type";
type.SqlDbType = SqlDbType.NVarChar;
type.Value = "D";
myCommand.Parameters.Add(type);
SqlParameter extensionId = new SqlParameter();
extensionId.ParameterName = "@extensionId";
extensionId.SqlDbType = SqlDbType.Int;
extensionId.Value = "1";
myCommand.Parameters.Add(extensionId);
SqlParameter parentId = new SqlParameter();
parentId.ParameterName = "@parentsId";
parentId.SqlDbType = SqlDbType.Int;
parentId.Value = GetParentId(cheminParent);
myCommand.Parameters.Add(parentId);
SqlParameter descr = new SqlParameter();
descr.ParameterName = "@description";
descr.SqlDbType = SqlDbType.NVarChar;
descr.Value = nomdossier;
myCommand.Parameters.Add(descr);
myCommand.ExecuteNonQuery();
}
///
/// Methode renvoyant l'id du fichier suivant son chemin
///
/// chemin du fichier
/// Id du fichier
public int GetParentId(string chemin)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = myConnection.CreateCommand();
try
{
int count = 0;
myConnection.Open();
myCommand.CommandText = "GetParentId";
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter path = new SqlParameter();
path.ParameterName = "@path";
path.SqlDbType = SqlDbType.VarChar;
path.Value = chemin;
myCommand.Parameters.Add(path);
if (myCommand.ExecuteScalar() != null)
{
count = (int)myCommand.ExecuteScalar();
return count;
}
else
{
return count;
}
}
catch
{
return 0;
}
finally
{
myConnection.Close();
}
}
///
/// Ouvrir le fichier
///
///
///
protected void bOuvrir_Click(object sender, EventArgs e)
{
int fid = Int32.Parse(DataList1.SelectedValue.ToString());
restaurer(fid, 0);
}
///
/// DropDown du dossier lors d'un changement
///
///
///
protected void DropDownDossier_SelectedIndexChanged(object sender, EventArgs e)
{
dossierId = int.Parse(DropDownDossier.SelectedValue.ToString());
affichage(1);
}
///
/// DropDown de l'extension lors d'un changement
///
///
///
protected void DropDownExtension_SelectedIndexChanged(object sender, EventArgs e)
{
extId = int.Parse(DropDownExtension.SelectedValue.ToString());
affichage(1);
}
///
/// Bouton Upload
///
///
///
protected void bUpload_Click(object sender, EventArgs e)
{
Response.Redirect("~/1/UploadsDocuments.aspx?dossierId="+dossierId);
}
///
/// Nombre de fichier dans le dossier
///
/// La valeur du nombre de fichier dans le dossier
public int numberOfFilesInDir()
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
try
{
// attention ALL
string query = "";
if (extId != 9)
{
query = "SELECT count(*) FROM fichiers WHERE type='F' AND parentsId = @parId AND extensionId = @ext";
}
else
{
query = "SELECT count(*) FROM fichiers WHERE type='F' AND parentsId = @parId";
}
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlParameter parId = new SqlParameter();
parId.ParameterName = "@parId";
parId.SqlDbType = SqlDbType.Int;
parId.Value = dossierId;
myCommand.Parameters.Add(parId);
SqlParameter ext = new SqlParameter();
ext.ParameterName = "@ext";
ext.SqlDbType = SqlDbType.Int;
ext.Value = extId;
myCommand.Parameters.Add(ext);
return (int)myCommand.ExecuteScalar();
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
myConnection.Close();
}
return 0;
}
///
/// Nombre de TOUS les fichier present dans le dossier et les sous dossiers
///
/// La valeur du nombre de TOUS les fichier dans le dossier et sous dossiers
public int numberOfFilesInTree()
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
try
{
// attention ALL
string query = "";
string path = getPathDossier(dossierId);
if (extId != 9)
{
query = "SELECT count(*) FROM fichiers WHERE type='F' AND path like '" + path + "%' AND extensionId = @ext";
}
else
{
query = "SELECT count(*) FROM fichiers WHERE type='F' AND path like '"+path+"%'";
}
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlParameter ext = new SqlParameter();
ext.ParameterName = "@ext";
ext.SqlDbType = SqlDbType.Int;
ext.Value = extId;
myCommand.Parameters.Add(ext);
return (int)myCommand.ExecuteScalar();
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
myConnection.Close();
}
return 0;
}
///
/// Reaffichage lors de changement dans les drop down
///
public void changeExtOrDir(int load)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
try
{
String query = "";
if (load == 0)
{
extId = 9;
parentId = 1;
}
else
{
extId = int.Parse(DropDownExtension.SelectedValue.ToString());
parentId = int.Parse(DropDownDossier.SelectedValue.ToString());
}
// 9 == ALL
if (extId == 9)
{
//Response.Write("ALL detected" + DropDownExtension.SelectedValue);
query = "SELECT fichiersId,nom,path,dateFichier,type,description,parentsId,extensionId FROM [fichiers] WHERE ([type] = 'F') AND ([parentsId] = @parId )";
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlParameter parId = new SqlParameter();
parId.ParameterName = "@parId";
parId.SqlDbType = SqlDbType.Int;
parId.Value = parentId;
myCommand.Parameters.Add(parId);
SqlDataReader myReader = myCommand.ExecuteReader();
if (!myReader.HasRows)
{
DataList1.Visible = false;
}
else
{
DataList1.DataSourceID = null;
DataList1.DataSource = myReader;
DataList1.DataBind();
DataList1.Visible = true;
}
}
else
{
//Response.Write(extId+" detected" + DropDownExtension.SelectedValue);
query = "SELECT fichiersId,nom,path,dateFichier,type,description,parentsId,extensionId FROM [fichiers] WHERE ([type] = 'F') AND ([parentsId] = @parId )AND ([extensionId] = @ext)";
SqlParameter ext = new SqlParameter();
ext.ParameterName = "@ext";
ext.SqlDbType = SqlDbType.Int;
ext.Value = extId;
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.Add(ext);
SqlParameter parId = new SqlParameter();
parId.ParameterName = "@parId";
parId.SqlDbType = SqlDbType.Int;
parId.Value = parentId;
myCommand.Parameters.Add(parId);
SqlDataReader myReader = myCommand.ExecuteReader();
if (!myReader.HasRows)
{
DataList1.Visible = false;
}
else
{
DataList1.DataSourceID = null;
DataList1.DataSource = myReader;
DataList1.DataBind();
DataList1.Visible = true;
}
}
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
myConnection.Close();
}
}
///
/// Fournis le chemin du dossier suivant l'id
///
/// Id du dossier en INT
/// Retourne Le chemin du dossier
public string getPathDossier(int id)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
string path = "";
try
{
String query = "SELECT path FROM fichiers WHERE fichiersId = @fId";
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlParameter fId = new SqlParameter();
fId.ParameterName = "@fId";
fId.SqlDbType = SqlDbType.Int;
fId.Value = id;
myCommand.Parameters.Add(fId);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.HasRows)
{
myReader.Read();
}
path = myReader.GetValue(0).ToString();
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
myConnection.Close();
}
return path;
}
///
/// Si tu clique sur le grid view du sous dossier tu le selectionne.
///
///
///
protected void gvSousDossier_SelectedIndexChanged(object sender, EventArgs e)
{
dossierId = int.Parse(gvSousDossier.SelectedValue.ToString());
DropDownDossier.SelectedValue = gvSousDossier.SelectedValue.ToString();
affichage(1);
}
///
/// Label du chemin, lors d'un clik retourne au dossier parent
///
///
///
protected void lChemin_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
parentId = renvoyeParentId(int.Parse(DropDownDossier.SelectedValue));
try
{
if (parentId > 0)
{
String query = "SELECT nom FROM fichiers WHERE fichiersId =" + parentId;
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.HasRows)
{
myReader.Read();
}
DropDownDossier.SelectedValue = parentId.ToString();
dossierId = parentId;
affichage(1);
}
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
myConnection.Close();
}
}
///
/// Renvoie le parents id du fichier
///
/// Reçois l'Id du fichier
/// Le Parent Id du fichier
public int renvoyeParentId(int fid)
{
try
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
string query = "SELECT parentsId FROM fichiers WHERE fichiersId=" + fid;
myConnection.Open();
SqlCommand myCommand = new SqlCommand(query, myConnection);
return (int)myCommand.ExecuteScalar();
}
catch
{
return 0;
}
}
///
/// Affiche les labels lChemin, lCountFichier, lCountAll et fait appel à la methode changeExtOrdir
///
/// 0 debut programme/1 programme en cours
public void affichage(int temp)
{
lChemin.Text = getPathDossier(parentId);
lCountFichier.Text = numberOfFilesInDir().ToString();
lCountAll.Text = numberOfFilesInTree().ToString();
changeExtOrDir(temp);
}
}