TFS_ANTIGUO/SIGPC/ProyectoSIGPC/FileUploadHandler.ashx
kledezma 7fdadc4709 KLB
git-tfs-id: [http://192.168.1.10:8080/tfs/AyA]$/SINORT;C519
2021-01-22 15:05:35 +00:00

169 lines
8 KiB
Text

<%@ WebHandler Language="C#" Class="FileUploadHandler" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using System.Web.Script.Serialization;
/// <summary>
/// Descripción breve de FileUploadHandler
/// </summary>
public class FileUploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
if (context.Request.QueryString["upload"] != null && context.Request.QueryString["solicitud"] != null && context.Request.QueryString["fkrequisito"] != null && context.Request.QueryString["pksolicitud"] != null && context.Request.QueryString["texto"] != null && context.Request.QueryString["origen"] != null)
{
//string pathrefer = context.Request.UrlReferrer.ToString();
string Serverpath = ConfigurationManager.AppSettings["DocumentosPDF"].ToString(); // HttpContext.Current.Server.MapPath("DocumentosPDF");
Serverpath += "\\" + context.Request.QueryString["solicitud"].ToString() + "\\DocumentosREQ";
var postedFile = context.Request.Files[0];
string ext = Path.GetExtension(postedFile.FileName);
//if (ext == ".pdf" || ext == ".PDF")
{
string file;
//For IE to get file name
if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
{
string[] files = postedFile.FileName.Split(new char[] { '\\' });
file = files[files.Length - 1].Replace(" ","_");
}
else
{
file = postedFile.FileName.Replace(" ", "_");
}
if (!Directory.Exists(Serverpath))
Directory.CreateDirectory(Serverpath);
string fileDirectory = Serverpath;
if (context.Request.QueryString["fileName"] != null)
{
file = context.Request.QueryString["fileName"].Replace(" ", "_");
if (File.Exists(fileDirectory + "\\" + file))
{
File.Delete(fileDirectory + "\\" + file);
}
}
//string ext = Path.GetExtension(fileDirectory + "\\" + file);
//file = Guid.NewGuid() + ext;
fileDirectory = Serverpath + "\\" + file;
if (context.Request.QueryString["DOCFISICO"] != null)
fileDirectory = Serverpath + "\\" + context.Request.QueryString["solicitud"].ToString() + "_DF.pdf";
if (File.Exists(fileDirectory) == false)
{
postedFile.SaveAs(fileDirectory);
context.Response.AddHeader("Vary", "Accept");
try
{
if (context.Request["HTTP_ACCEPT"].Contains("application/json"))
context.Response.ContentType = "application/json";
else
context.Response.ContentType = "text/plain";
}
catch
{
context.Response.ContentType = "text/plain";
}
int nPKDocumento = 0;
if (context.Request.QueryString["origen"] == "requisito")
{
AyABL.TRA.documentoBL objdocumentoBL = new AyABL.TRA.documentoBL();
objdocumentoBL.objdocumentoEL.Descripcion = context.Request.QueryString["texto"];
objdocumentoBL.objdocumentoEL.NOMBRE = postedFile.FileName.Replace(" ", "_");
objdocumentoBL.objdocumentoEL.FKREQUISITO = int.Parse(context.Request.QueryString["fkrequisito"]);
objdocumentoBL.objdocumentoEL.FKSOLICITUD = int.Parse(context.Request.QueryString["pksolicitud"]);
nPKDocumento = objdocumentoBL.insertarDocumento();
}
if (context.Request.QueryString["origen"] == "adjunto")
{
AyABL.Tran.adjuntoBL objdocumentoBL = new AyABL.Tran.adjuntoBL();
objdocumentoBL.objadjuntoEL.NOMBRE = postedFile.FileName;
objdocumentoBL.objadjuntoEL.DESCRIPCION = context.Request.QueryString["texto"];
objdocumentoBL.objadjuntoEL.FKSOLICITUD = int.Parse(context.Request.QueryString["pksolicitud"]);
nPKDocumento = objdocumentoBL.insertarAdjunto();
}
//if (nPKDocumento != 0)
//{
// //context.Response.Write("Success");
// context.Response.ContentType = "text/plain";
// context.Response.Write(file);
//}
//else
//{
// File.Delete(fileDirectory);
//}
if(File.Exists(fileDirectory))
{
context.Response.ContentType = "text/plain";
context.Response.Write(file);
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("falloGuardar");
}
return;
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("falloExiste");
return;
}
}
//else
//{
// context.Response.Write("errorEXT");
// return;
//}
}
if (context.Request.QueryString["pkeliminar"] != null && context.Request.QueryString["solicitud"] != null)
{
string Serverpath = HttpContext.Current.Server.MapPath("DocumentosPDF");
Serverpath += "\\" + context.Request.QueryString["solicitud"].ToString() + "\\DocumentosREQ";
string file = context.Request.QueryString["pkeliminar"];
string fileDirectory = Serverpath + "\\" + file;
//File.Delete(fileDirectory);
var wrapper = new { d = "exitoso" };
context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(wrapper));
return;
}
}
catch (Exception exp)
{
context.Response.Write(exp.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}