141 lines
4.0 KiB
C#
141 lines
4.0 KiB
C#
using AppData;
|
|
using SteamWare;
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace NKC_WF.WebUserContols
|
|
{
|
|
public partial class cmp_fileUpload : BaseUserControl
|
|
{
|
|
/// <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 aprtire dalla tab diritti...
|
|
/// </summary>
|
|
public bool userIsAuth
|
|
{
|
|
get
|
|
{
|
|
return true;
|
|
//return (idxAmm + idxFornitore > 0);
|
|
}
|
|
}
|
|
public bool isWriteEnabled
|
|
{
|
|
get
|
|
{
|
|
bool answ = false;
|
|
bool.TryParse(hfWriteEnabled.Value, out answ);
|
|
return answ;
|
|
}
|
|
set
|
|
{
|
|
hfWriteEnabled.Value = value.ToString();
|
|
}
|
|
}
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (!IsPostBack)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
|
|
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);
|
|
string dirTo = $"{_SqlCopyDir}{adesso.ToString("yyyy-MM")}\\";
|
|
string dirImport = $"{_SqlImportDir}{adesso.ToString("yyyy-MM")}\\";
|
|
// se c'è un file
|
|
if (FileUpload1.PostedFile != null)
|
|
{
|
|
uploadedFilename = Path.GetFileName(FileUpload1.PostedFile.FileName.Replace(" ", "_"));
|
|
csvFileName = $"{adesso.ToString("dd-HHmmss")}_{uploadedFilename}";
|
|
batchName = uploadedFilename.Replace(".csv", "");
|
|
savedFilename = Server.MapPath($"{_tempUploadDir}{csvFileName}");
|
|
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();
|
|
}
|
|
try
|
|
{
|
|
// copio su SQL...
|
|
fileMover.obj.copiaFile(dirFrom, dirTo, csvFileName);
|
|
// chiamo procedura SQL x import...
|
|
DataLayer.man.taImpLog.importCsvOrd($"{dirImport}", csvFileName, batchName, ";", "\n", "2", 0, 0, 0);
|
|
raiseEvent();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
logger.lg.scriviLog($"Eccezione in Upload file:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
Response.Redirect(Request.RawUrl);
|
|
}
|
|
|
|
|
|
|
|
protected void btnAdd_Click(object sender, EventArgs e)
|
|
{
|
|
fisVisFU(true);
|
|
btnUpload.Text = "Carica Nuovo";
|
|
}
|
|
/// <summary>
|
|
/// fix visibilità controllo file upload
|
|
/// </summary>
|
|
/// <param name="showAdd">mostra controlli add=true oppure nasconde = false</param>
|
|
private void fisVisFU(bool showAdd)
|
|
{
|
|
divFileUpl.Visible = showAdd;
|
|
btnAdd.Visible = !showAdd;
|
|
btnCancel.Visible = showAdd;
|
|
}
|
|
|
|
protected void btnCancel_Click(object sender, EventArgs e)
|
|
{
|
|
fisVisFU(false);
|
|
}
|
|
|
|
}
|
|
} |