Files
SSC/CMS_SC/WebUserControls/mod_fileUpload.ascx.cs
Samuele E. Locatelli ed52b02fad Fix file upload...
2018-02-21 18:05:26 +01:00

166 lines
4.5 KiB
C#

using CMS_SC_Data;
using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
namespace CMS_SC.WebUserControls
{
public partial class mod_fileUpload : SteamWare.UserControl
{
/// <summary>
/// selezione valore in DettScheda
/// </summary>
public event EventHandler eh_fileMod;
/// <summary>
/// sollevo evento selezione
/// </summary>
protected void raiseEvent()
{
// sollevo evento nuovo valore...
if (eh_fileMod != null)
{
eh_fileMod(this, new EventArgs());
}
}
/// <summary>
/// abilita modifica/cancellazione allegati
/// </summary>
public bool enableMod { get; set; }
/// <summary>
/// caricamento pagina
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Form.Enctype = "multipart/form-data";
fisVisFU(enableMod);
if (!IsPostBack)
{
fisVisFU(false);
grView.DataBind();
}
divNewEdit.Visible = isWriteEnabled && enableMod;
}
/// <summary>
/// Chiave di filtraggio dati...
/// </summary>
public string filtKey
{
get
{
return hfFiltKey.Value;
}
set
{
hfFiltKey.Value = value;
}
}
protected void Upload(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
string filename = Path.GetFileName(FileUploadControl.PostedFile.FileName);
string contentType = FileUploadControl.PostedFile.ContentType;
using (Stream fs = FileUploadControl.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
// PROCEDO SOLO SE HO UN FILE!!!! ovvero bytes <> null
if (bytes.Length > 0)
{
// controllo se sia caricamento o update...
if (grView.SelectedIndex != -1)
{
try
{
int orig_idxFile = Convert.ToInt32(grView.SelectedValue);
DtProxy.man.taFiles.updateQuery(filename, filtKey, contentType, bytes, orig_idxFile);
}
catch
{ }
}
else
{
DtProxy.man.taFiles.insertQuery(filename, filtKey, contentType, bytes);
}
}
}
}
grView.DataBind();
raiseEvent();
}
}
protected void DownloadFile(object sender, EventArgs e)
{
int idxFile = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
CMS_SC_Data.DS_Utility.tblFilesRow fileRow = DtProxy.man.taFiles.getByKey(idxFile)[0];
bytes = fileRow.Data;
contentType = fileRow.ContentType;
fileName = fileRow.FileName;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
protected void grView_SelectedIndexChanged(object sender, EventArgs e)
{
// mostro controllo x edit...
fisVisFU(true);
}
/// <summary>
/// fix visibilità controllo file upload
/// </summary>
/// <param name="showAdd">mostra controlli add=true oppure nasconde = false</param>
private void fisVisFU(bool showAdd)
{
bool fileSel = grView.SelectedIndex >= 0;
FileUploadControl.Visible = showAdd;
lbtAddNew.Visible = !showAdd;
lbtUpload.Visible = showAdd && !fileSel;
lbtReplace.Visible = showAdd && fileSel;
lbtCancel.Visible = showAdd;
if (grView.SelectedIndex != -1 && !showAdd)
{
grView.SelectedIndex = -1;
}
}
protected void lbtAddNew_Click(object sender, EventArgs e)
{
fisVisFU(true);
}
protected void lbtCancel_Click(object sender, EventArgs e)
{
fisVisFU(false);
}
protected void btnReset_Click(object sender, EventArgs e)
{
resetSelezione();
}
/// <summary>
/// resetto selezione
/// </summary>
private void resetSelezione()
{
grView.SelectedIndex = -1;
grView.DataBind();
fisVisFU(false);
}
}
}