TFS_ANTIGUO/AYA.SlnSynor/AYA.SlnSynor/FileUploadHandler/FileUploadHandler.ashx.cs

108 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using System.Web.Script.Serialization;
namespace webAgenciaVirtual
{
/// <summary>
/// Descripción breve de FileUploadHandler
/// </summary>
public class FileUploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string pathrefer = context.Request.UrlReferrer.ToString();
//string Serverpath = HttpContext.Current.Server.MapPath("DocumentosSINORT");
string Serverpath = ConfigurationManager.AppSettings["FileRepository"].ToString();
if (context.Request.QueryString["id"] != null){
Serverpath += context.Request.QueryString["tipo"].ToString() + "\\" + context.Request.QueryString["id"].ToString() + "\\" + context.Request.QueryString["carpeta"].ToString();
}else
{
Serverpath += context.Request.QueryString["tipo"].ToString();
}
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(" ", "_");
}
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;
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";
}
//context.Response.Write("Success");
context.Response.ContentType = "text/plain";
context.Response.Write(file);
return;
//}
//else
//{
// context.Response.Write("errorEXT");
// return;
//}
}
catch (Exception exp)
{
context.Response.Write(exp.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}