using SteamWare; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MP_ADM.WebUserControls { public partial class cmp_fileUpload : BaseUserControl { #region Protected Fields /// /// Folder salvataggio file /// protected string _SqlCopyDir = memLayer.ML.CRS("_SqlCopyDir"); /// /// Folder di upload temporaneo files /// protected string _tempUploadDir = memLayer.ML.CRS("_tempUploadDir"); #endregion Protected Fields #region Public Events /// /// Evento upload e salvataggio su server IIS del file... /// public event EventHandler eh_FileUploaded; #endregion Public Events #region Public Properties /// /// Prefisso file abilitato /// public string filePrefix { get { return hfFilePrefix.Value; } set { hfFilePrefix.Value = value; } } /// /// Suffisso file abilitato (=tipo) /// public string fileSuffix { get { return hfFileSuffix.Value; } set { hfFileSuffix.Value = value; } } /// /// Indica se fare redirect post upload /// public bool forceRedirect { get { bool answ = true; bool.TryParse(hfForceRedirect.Value, out answ); return answ; } set { hfForceRedirect.Value = value.ToString(); } } public bool isWriteEnabled { get { bool answ = false; bool.TryParse(hfWriteEnabled.Value, out answ); return answ; } set { hfWriteEnabled.Value = value.ToString(); } } /// /// permesso scrittura SE E' abilitato a partire dalla tab diritti... /// public bool userIsAuth { get { return true; //return (idxAmm + idxFornitore > 0); } } #endregion Public Properties #region Private Methods /// /// Procedee a bonificare la cartella di upload dei files + vecchi di 3 mesi /// private void deleteOldFiles() { string dirPath = Server.MapPath(_tempUploadDir); // elenco files nella directory string[] files = Directory.GetFiles(dirPath); // li guardo tutti e se vecchi li elimino... foreach (string file in files) { FileInfo fi = new FileInfo(file); if (fi.LastAccessTime < DateTime.Now.AddMonths(-1)) fi.Delete(); } } /// /// fix visibilità controllo file upload /// /// mostra controlli add=true oppure nasconde = false private void fisVisFU(bool showAdd) { divFileUpl.Visible = showAdd; btnAdd.Visible = !showAdd; lbtCancel.Visible = showAdd; } #endregion Private Methods #region Protected Methods protected void btnAdd_Click(object sender, EventArgs e) { fisVisFU(true); lbtUpload.Text = traduci("AddNew"); } protected void btnCancel_Click(object sender, EventArgs e) { Response.Redirect(Request.RawUrl); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { divMessagge.Visible = false; fisVisFU(false); deleteOldFiles(); } divNewEdit.Visible = userIsAuth && isWriteEnabled; } protected void Upload(object sender, EventArgs e) { string uplFileName = ""; string uploadedFilename = ""; string batchName = ""; string savedFilename = ""; string contentType = ""; DateTime adesso = DateTime.Now; string dirFrom = Server.MapPath(_tempUploadDir); // se c'è un file if (FileUpload1.PostedFile != null) { uploadedFilename = Path.GetFileName(FileUpload1.PostedFile.FileName.Replace(" ", "_")).ToLower(); // verifico suffisso sia corretto... if (!string.IsNullOrEmpty(fileSuffix) && !uploadedFilename.EndsWith(fileSuffix.ToLower())) { divMessagge.Visible = true; lblMessage.Text = $"{traduci("FileSuffixError")} {fileSuffix}"; } else { // se sono a validare dxf... if (fileSuffix == ".dxf") { uplFileName = uploadedFilename; batchName = uploadedFilename; } else { // altrimenti per csv CNC/KIT.. uplFileName = $"{adesso.ToString("dd-HHmmss")}_{uploadedFilename}"; batchName = uploadedFilename.Replace(fileSuffix, "").Replace("cnc", "").Replace("kit", ""); } savedFilename = Server.MapPath($"{_tempUploadDir}{uplFileName}"); divMessagge.Visible = false; // verifica preliminare del PRE del name... if (!string.IsNullOrEmpty(filePrefix) && !uploadedFilename.StartsWith(filePrefix.ToLower())) { divMessagge.Visible = true; lblMessage.Text = $"{traduci("FilePrefixError")} {filePrefix}"; } else { contentType = FileUpload1.PostedFile.ContentType; // accedo allo stream del file allegato using (Stream fs = FileUpload1.PostedFile.InputStream) { // scrivo su file FileStream file = new FileStream(savedFilename, FileMode.Create, FileAccess.Write); fs.CopyTo(file); file.Close(); // log! logger.lg.scriviLog($"Effettuato salvataggio file {uploadedFilename} come {savedFilename}"); } // segnaloc he ho CARICATO IN LOCALE il file reportFileUploaded(new FileUploadEventArgs(dirFrom, uplFileName, batchName)); } } } if (forceRedirect) { Response.Redirect(Request.RawUrl); } } #endregion Protected Methods #region Public Methods /// /// Solelva evento salvataggio file con messaggio path/nome /// /// /// public void reportFileUploaded(FileUploadEventArgs e) { EventHandler handler = eh_FileUploaded; if (handler != null) { handler(this, e); } } #endregion Public Methods } public class FileUploadEventArgs : EventArgs { #region Private Fields private string _batchName; private string _fileName; private string _localPath; #endregion Private Fields #region Public Constructors /// /// Evento con argomenti soppevato post fileUpload /// /// /// /// public FileUploadEventArgs(string localPath, string fileName, string batchName) { _localPath = localPath; _fileName = fileName; _batchName = batchName; } #endregion Public Constructors #region Public Properties public string BatchName { get { return _batchName; } set { _batchName = value; } } public string FileName { get { return _fileName; } set { _fileName = value; } } public string LocalPath { get { return _localPath; } set { _localPath = value; } } #endregion Public Properties } }