Files

202 lines
5.3 KiB
C#

using SteamWare;
using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
namespace GIM_site.WebUserControls
{
public partial class mod_fileUpload : SteamWare.UserControl
{
/// <summary>
/// abilita modifica/cancellazione allegati
/// </summary>
public bool enableMod { get; set; }
public bool showOpenNewTab
{
get
{
return divOpenNew.Visible;
}
set
{
divOpenNew.Visible = value;
divNormal.Visible = !value;
}
}
/// <summary>
/// caricamento pagina
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (memLayer.ML.QSI("numIntMtz") != 0)
{
numIntMtz = memLayer.ML.QSI("numIntMtz");
}
hlOpenNew.NavigateUrl = string.Format("~/AllegatiRichiesta?numIntMtz={0}", numIntMtz);
hlOpenNew.Enabled = !enableMod;
fisVisFU(false);
grView.DataBind();
divNewEdit.Visible = enableMod; // isWriteEnabled;
btnAdd.DataBind();
}
}
/// <summary>
/// Num Int richiesto
/// </summary>
public int numIntMtz
{
get
{
int answ = 0;
try
{
answ = Convert.ToInt32(hfNumIntMtz.Value);
}
catch
{ }
return answ;
}
set
{
hfNumIntMtz.Value = value.ToString();
}
}
public void doUpdate()
{
grView.DataBind();
}
public int numAllegati
{
get
{
int answ = 0;
try
{
//answ = DtProxy.man.taFile.getByImpegno(numIntMtz).Rows.Count;
answ = WebGimUtils.elencoFiles(numIntMtz).Count;
}
catch
{ }
return answ;
}
}
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.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)
{
fileMover.obj.setDirectoryMapPath(WebGimUtils.dirPathIntMtz(numIntMtz));
// controllo se sia caricamento o update...
if (grView.SelectedIndex != -1)
{
try
{
fileMover.obj.eliminaFile(grView.SelectedValue.ToString());
fileMover.obj.salvaFileBuffer(filename, bytes);
//int orig_idxFile = Convert.ToInt32(grView.SelectedValue);
//DtProxy.man.taFile.UpdateQuery(idxImpegno, 0, filename, contentType, bytes, orig_idxFile);
}
catch
{ }
}
else
{
fileMover.obj.salvaFileBuffer(filename, bytes);
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
public string fileUrl(object name)
{
return string.Format("{0}//{1}", WebGimUtils.dirPathIntMtz(numIntMtz), name);
}
protected void DownloadFile(object sender, EventArgs e)
{
byte[] bytes;
string fileName, contentType;
//int idxFile = int.Parse((sender as LinkButton).CommandArgument);
//DS_Utility.tblFilesRow fileRow = DtProxy.man.taFile.getByKey(idxFile)[0];
//bytes = fileRow.Data;
//contentType = fileRow.ContentType;
//fileName = fileRow.FileName;
fileName = ((sender as LinkButton).CommandArgument);
bytes = fileMover.obj.scaricaFile(fileName);
contentType = MimeMapping.GetMimeMapping(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);
btnUpload.Text = "Sostituisci File";
}
protected void btnAdd_Click(object sender, EventArgs e)
{
fisVisFU(true);
btnUpload.Text = "Carica Nuovo File";
}
/// <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;
if (grView.SelectedIndex != -1 && !showAdd)
{
grView.SelectedIndex = -1;
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
fisVisFU(false);
}
protected void odsFile_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
{
// cancello manualmente
fileMover.obj.setDirectoryMapPath(WebGimUtils.dirPathIntMtz(numIntMtz));
fileMover.obj.eliminaFile(e.InputParameters["Original_Nome"].ToString());
// fermo procedura...
e.Cancel = true;
}
}
}