using Data; using SteamWare; using System; using System.IO; using System.Web; using System.Web.UI.WebControls; namespace PUB.WebUserContols { public partial class cmp_fileUpload : System.Web.UI.UserControl { public int idxRichiesta { get { int answ = 0; int.TryParse(Request.QueryString["idxRichiesta"], out answ); return answ; } } public string filtroRich { get { string answ = "###"; if (idxRichiesta > 0) answ = idxRichiesta.ToString(); return answ; } } /// /// KEY amministratore in sessione /// protected int idxAmm { get { return memLayer.ML.IntSessionObj("idxAmm"); } } /// /// KEY fornitore in sessione /// protected int idxFornitore { get { return memLayer.ML.IntSessionObj("idxFornitore"); } } /// /// permesso scrittura SE E' un FORNITORE / AMMINISTRATORE /// public bool userIsAuth { get { 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); grView.DataBind(); } divNewEdit.Visible = userIsAuth && isWriteEnabled; } protected void Upload(object sender, EventArgs e) { if (FileUpload1.PostedFile != null) { 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); // controllo se sia caricamento o update... if (grView.SelectedIndex != -1) { try { int orig_idxFile = Convert.ToInt32(grView.SelectedValue); DtProxy.man.taFile.updateQuery(filtroRich, filename, contentType, bytes, orig_idxFile); } catch { } } else { DtProxy.man.taFile.insertQuery(filtroRich, filename, contentType, bytes); } } } } //Response.Redirect(Request.Url.AbsoluteUri); Response.Redirect(Request.RawUrl); } protected void DownloadFile(object sender, EventArgs e) { int idxFile = int.Parse((sender as LinkButton).CommandArgument); byte[] bytes; string fileName, contentType; 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 = "Aggiorna File"; } protected void btnAdd_Click(object sender, EventArgs e) { fisVisFU(true); btnUpload.Text = "Carica Nuovo"; } /// /// fix visibilità controllo file upload /// /// mostra controlli add=true oppure nasconde = false 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); } } }