Files
2017-08-25 10:16:04 +02:00

112 lines
3.8 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 ScheMe_Data;
using System.Text;
using SteamWare;
namespace ScheMe.WebUserControls
{
public partial class mod_fileUpload : SteamWare.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fisVisFU(false);
grView.DataBind();
}
divNewEdit.Visible = isWriteEnabled;
}
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
string gruppo = memLayer.ML.StringSessionObj("Gruppo");
int idxPaziente = memLayer.ML.IntSessionObj("IdxPaziente");
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(gruppo, idxPaziente, filename, contentType, bytes, orig_idxFile);
}
catch
{ }
}
else
{
DtProxy.man.taFile.InsertQuery(gruppo, idxPaziente, 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;
ScheMe_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";
}
/// <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);
}
}
}