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 { /// /// selezione valore in DettScheda /// public event EventHandler eh_fileMod; /// /// sollevo evento selezione /// protected void raiseEvent() { // sollevo evento nuovo valore... if (eh_fileMod != null) { eh_fileMod(this, new EventArgs()); } } /// /// abilita modifica/cancellazione allegati /// public bool enableMod { get; set; } /// /// caricamento pagina /// /// /// 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; } /// /// Chiave di filtraggio dati... /// 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); } /// /// fix visibilità controllo file upload /// /// mostra controlli add=true oppure nasconde = false 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(); } /// /// resetto selezione /// private void resetSelezione() { grView.SelectedIndex = -1; grView.DataBind(); fisVisFU(false); } } }