SIAQ.BusinessProcess.Object.BPDocumento.UploadFile C# (CSharp) Method

UploadFile() public method

Sube un archivo al servidor, regresando la ruta en donde se guardó físicamente
BPDocumento.UploadFile 09-Septiembre-2014 Ruben.Cobos
public UploadFile ( System.Web.HttpPostedFile PostedFile, String Seed, RepositoryTypes RepositoryType ) : String
PostedFile System.Web.HttpPostedFile Archivo a subir
Seed String Semilla el directorio. ID de Expediente o Solicitud
RepositoryType RepositoryTypes Tipo de repositorio (Expediente o Solicitud)
return String
        public String UploadFile( HttpPostedFile PostedFile,  String Seed, RepositoryTypes RepositoryType)
        {
            String Path;
            String FileName;

            try{

                // Nombre del archivo físico
                FileName = PostedFile.FileName;

                // Armar el path
                switch(RepositoryType){
                    case RepositoryTypes.Expediente:
                        Path = ConfigurationManager.AppSettings["Application.Repository"].ToString() + "E" + Seed + Convert.ToChar(92);
                        break;

                    case RepositoryTypes.Solicitud:
                        Path = ConfigurationManager.AppSettings["Application.Repository"].ToString() + "S" + Seed + Convert.ToChar(92);
                        break;

                    case RepositoryTypes.Recomendacion:
                        Path = ConfigurationManager.AppSettings["Application.Repository"].ToString() + "R" + Seed + Convert.ToChar(92);
                        break;

                    default:
                        throw( new Exception("Tipo de repositorio inválido"));
                }

                // Validar existencia de la ruta
                if (!Directory.Exists(Path)) { Directory.CreateDirectory(Path); }

                // Validar la existencia del archivo
                if (File.Exists(Path + FileName)) { throw( new Exception("Ya existe éste archivo asociado al expediente")); }

                // Cargar el archivo
                PostedFile.SaveAs(Path + FileName);

            }catch (IOException ioEx){

                throw(ioEx);

            } catch (Exception ex){

                throw(ex);

            }

            // Directorio con nombre de archivo
            return Path + FileName;
        }

Usage Example

Exemplo n.º 1
0
        void InsertDocumento()
        {
            ENTDocumento oENTDocumento = new ENTDocumento();
            ENTResponse oENTResponse = new ENTResponse();
            ENTSession oENTSession;

            BPDocumento oBPDocumento = new BPDocumento();

            try
            {

                // Validaciones
                if (this.fupArchivo.PostedFile == null) { throw (new Exception("Es necesario seleccionar un Documento")); }
                if (!this.fupArchivo.HasFile) { throw (new Exception("Es necesario seleccionar un Documento")); }
                if (this.fupArchivo.PostedFile.ContentLength == 0) { throw (new Exception("Es necesario seleccionar un Documento")); }

                // Obtener Sesion
                oENTSession = (ENTSession)this.Session["oENTSession"];

                // Formulario
                oENTDocumento.SolicitudId = Int32.Parse(this.hddSolicitudId.Value);
                oENTDocumento.ExpedienteId = 0;
                oENTDocumento.ModuloId = 2; // Quejas
                oENTDocumento.idUsuarioInsert = oENTSession.idUsuario;
                oENTDocumento.Extension = Path.GetExtension(this.fupArchivo.PostedFile.FileName);
                oENTDocumento.Nombre = this.fupArchivo.FileName;
                oENTDocumento.Descripcion = this.ckeDescripcion.Text.Trim();
                oENTDocumento.Ruta = oBPDocumento.UploadFile(this.fupArchivo.PostedFile, this.hddSolicitudId.Value, BPDocumento.RepositoryTypes.Solicitud );

                // Transacción
                oENTResponse = oBPDocumento.InsertDocumento(oENTDocumento);

                // Errores y Warnings
                if (oENTResponse.GeneratesException) { throw (new Exception(oENTResponse.sErrorMessage)); }
                if (oENTResponse.sMessage != "") { throw (new Exception(oENTResponse.sMessage)); }

                // Estado inicial del formulario
                this.ckeDescripcion.Text = "";

                // Refrescar el formulario
                SelectSolicitud();

                // Foco
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), Convert.ToString(Guid.NewGuid()), "function pageLoad(){ focusControl('" + this.fupArchivo.ClientID + "'); }", true);

            }catch (Exception ex){
                throw (ex);
            }
        }