modifiche fileUpload x gestione dxf oltre a csv

This commit is contained in:
Samuele E. Locatelli
2020-11-26 12:08:19 +01:00
parent e73bfdf618
commit c98e2ee71f
3 changed files with 244 additions and 155 deletions
+2 -1
View File
@@ -3,6 +3,7 @@
<asp:HiddenField runat="server" ID="hfWriteEnabled" />
<asp:HiddenField runat="server" ID="hfForceRedirect" />
<asp:HiddenField runat="server" ID="hfFilePrefix" />
<asp:HiddenField runat="server" ID="hfFileSuffix" />
<div id="divFileUpl" runat="server" class="row">
<div class="col-12 col-sm-3 col-md-2">
<asp:LinkButton ID="lbtCancel" runat="server" Text="Annulla" OnClick="btnCancel_Click" CssClass="btn btn-warning form-control"><%: traduci("cancel") %></asp:LinkButton>
@@ -21,4 +22,4 @@
</div>
<div id="divMessagge" runat="server" class="bg-danger border border-danger text-center my-2">
<asp:Label runat="server" ID="lblMessage" CssClass="text-warning" />
</div>
</div>
+233 -154
View File
@@ -4,44 +4,38 @@ using System.IO;
namespace NKC_WF.WebUserContols
{
public class FileUploadEventArgs : EventArgs
{
/// <summary>
/// Evento con argomenti soppevato post fileUpload
/// </summary>
/// <param name="localPath"></param>
/// <param name="fileName"></param>
/// <param name="batchName"></param>
public FileUploadEventArgs(string localPath, string fileName, string batchName)
{
_localPath = localPath;
_fileName = fileName;
_batchName = batchName;
}
private string _localPath;
private string _fileName;
private string _batchName;
public string LocalPath
{
get { return _localPath; }
set { _localPath = value; }
}
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
public string BatchName
{
get { return _batchName; }
set { _batchName = value; }
}
}
public partial class cmp_fileUpload : BaseUserControl
{
#region Protected Fields
/// <summary>
/// Folder REMOTA x copia verso SQL
/// </summary>
protected string _SqlCopyDir = memLayer.ML.CRS("_SqlCopyDir");
/// <summary>
/// Folder x SQL import
/// </summary>
protected string _SqlImportDir = memLayer.ML.CRS("_SqlImportDir");
/// <summary>
/// Folder di upload temporaneo files
/// </summary>
protected string _tempUploadDir = memLayer.ML.CRS("_tempUploadDir");
#endregion Protected Fields
#region Public Events
/// <summary>
/// Evento upload e salvataggio su server IIS del file...
/// </summary>
public event EventHandler<FileUploadEventArgs> eh_FileUploaded;
#endregion Public Events
#region Public Properties
/// <summary>
/// Prefisso file abilitato
/// </summary>
@@ -58,131 +52,20 @@ namespace NKC_WF.WebUserContols
}
/// <summary>
/// Evento upload e salvataggio su server IIS del file...
/// Suffisso file abilitato (=tipo)
/// </summary>
public event EventHandler<FileUploadEventArgs> eh_FileUploaded;
/// <summary>
/// Solelva evento salvataggio file con messaggio path/nome
/// </summary>
/// <param name="localPath"></param>
/// <param name="fileName"></param>
public void reportFileUploaded(FileUploadEventArgs e)
{
EventHandler<FileUploadEventArgs> handler = eh_FileUploaded;
if (handler != null)
{
handler(this, e);
}
}
/// <summary>
/// Folder di upload temporaneo files
/// </summary>
protected string _tempUploadDir = memLayer.ML.CRS("_tempUploadDir");
/// <summary>
/// Folder REMOTA x copia verso SQL
/// </summary>
protected string _SqlCopyDir = memLayer.ML.CRS("_SqlCopyDir");
/// <summary>
/// Folder x SQL import
/// </summary>
protected string _SqlImportDir = memLayer.ML.CRS("_SqlImportDir");
/// <summary>
/// permesso scrittura SE E' abilitato a partire dalla tab diritti...
/// </summary>
public bool userIsAuth
public string fileSuffix
{
get
{
return true;
//return (idxAmm + idxFornitore > 0);
}
}
public bool isWriteEnabled
{
get
{
bool answ = false;
bool.TryParse(hfWriteEnabled.Value, out answ);
return answ;
return hfFileSuffix.Value;
}
set
{
hfWriteEnabled.Value = value.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
divMessagge.Visible = false;
fisVisFU(false);
deleteOldFiles();
}
divNewEdit.Visible = userIsAuth && isWriteEnabled;
}
/// <summary>
/// Procedee a bonificare la cartella di upload dei files + vecchi di 3 mesi
/// </summary>
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(-3))
fi.Delete();
hfFileSuffix.Value = value;
}
}
protected void Upload(object sender, EventArgs e)
{
string csvFileName = "";
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();
csvFileName = $"{adesso.ToString("dd-HHmmss")}_{uploadedFilename}";
batchName = uploadedFilename.Replace(".csv", "").Replace("cnc", "").Replace("kit", "");
savedFilename = Server.MapPath($"{_tempUploadDir}{csvFileName}");
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();
}
// segnaloc he ho CARICATO IN LOCALE il file
reportFileUploaded(new FileUploadEventArgs(dirFrom, csvFileName, batchName));
}
}
if (forceRedirect)
{
Response.Redirect(Request.RawUrl);
}
}
/// <summary>
/// Indica se fare redirect post upload
/// </summary>
@@ -200,11 +83,53 @@ namespace NKC_WF.WebUserContols
}
}
protected void btnAdd_Click(object sender, EventArgs e)
public bool isWriteEnabled
{
fisVisFU(true);
lbtUpload.Text = traduci("AddNew");
get
{
bool answ = false;
bool.TryParse(hfWriteEnabled.Value, out answ);
return answ;
}
set
{
hfWriteEnabled.Value = value.ToString();
}
}
/// <summary>
/// permesso scrittura SE E' abilitato a partire dalla tab diritti...
/// </summary>
public bool userIsAuth
{
get
{
return true;
//return (idxAmm + idxFornitore > 0);
}
}
#endregion Public Properties
#region Private Methods
/// <summary>
/// Procedee a bonificare la cartella di upload dei files + vecchi di 3 mesi
/// </summary>
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(-3))
fi.Delete();
}
}
/// <summary>
/// fix visibilità controllo file upload
/// </summary>
@@ -216,10 +141,164 @@ namespace NKC_WF.WebUserContols
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 uploadedFileName = "";
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")
{
batchName = uploadedFilename;
}
else
{
// altrimenti per csv CNC/KIT..
uploadedFileName = $"{adesso.ToString("dd-HHmmss")}_{uploadedFilename}";
batchName = uploadedFilename.Replace(fileSuffix, "").Replace("cnc", "").Replace("kit", "");
}
savedFilename = Server.MapPath($"{_tempUploadDir}{uploadedFileName}");
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();
}
// segnaloc he ho CARICATO IN LOCALE il file
reportFileUploaded(new FileUploadEventArgs(dirFrom, uploadedFileName, batchName));
}
}
}
if (forceRedirect)
{
Response.Redirect(Request.RawUrl);
}
}
#endregion Protected Methods
#region Public Methods
/// <summary>
/// Solelva evento salvataggio file con messaggio path/nome
/// </summary>
/// <param name="localPath"></param>
/// <param name="fileName"></param>
public void reportFileUploaded(FileUploadEventArgs e)
{
EventHandler<FileUploadEventArgs> 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
/// <summary>
/// Evento con argomenti soppevato post fileUpload
/// </summary>
/// <param name="localPath"></param>
/// <param name="fileName"></param>
/// <param name="batchName"></param>
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
}
}
+9
View File
@@ -41,6 +41,15 @@ namespace NKC_WF.WebUserContols
/// </remarks>
protected global::System.Web.UI.WebControls.HiddenField hfFilePrefix;
/// <summary>
/// Controllo hfFileSuffix.
/// </summary>
/// <remarks>
/// Campo generato automaticamente.
/// Per la modifica, spostare la dichiarazione di campo dal file di progettazione al file code-behind.
/// </remarks>
protected global::System.Web.UI.WebControls.HiddenField hfFileSuffix;
/// <summary>
/// Controllo divFileUpl.
/// </summary>