Files
WebSCR/WebSCR/WebUserControls/mod_fileUpload.ascx.cs
T
Samuele E. Locatelli 43d4609f23 refresh globale
2017-06-06 13:01:49 +02:00

161 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using WebSCR_data;
using System.Text;
using SteamWare;
namespace WebSCR.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)
{
hlOpenNew.NavigateUrl = string.Format("~/Allegati?IdxImpegno={0}", memLayer.ML.QSI("idxImpegno"));
fisVisFU(false);
grView.DataBind();
}
divNewEdit.Visible = isWriteEnabled;
btnAdd.DataBind();
}
public void doUpdate()
{
grView.DataBind();
}
public int numAllegati
{
get
{
int answ = 0;
try
{
answ = DtProxy.man.taFile.getByImpegno(memLayer.ML.QSI("idxImpegno")).Rows.Count;
}
catch
{ }
return answ;
}
}
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
int idxImpegno = memLayer.ML.QSI("idxImpegno");
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)
{
// controllo se sia caricamento o update...
if (grView.SelectedIndex != -1)
{
try
{
int orig_idxFile = Convert.ToInt32(grView.SelectedValue);
DtProxy.man.taFile.UpdateQuery(idxImpegno, 0, filename, contentType, bytes, orig_idxFile);
}
catch
{ }
}
else
{
DtProxy.man.taFile.InsertQuery(idxImpegno, 0, filename, contentType, bytes);
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void DownloadFile(object sender, EventArgs e)
{
int idxFile = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
WebSCR_data.DS_Utility.tblFilesRow fileRow = DtProxy.man.taFile.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);
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);
}
}
}