Пример #1
0
        public ActionResult UploadFileValidator(HttpPostedFileBase file = null)
        {
            string id_usuario = Request["user_id"];

            if (file != null)
            {
                DTOTransporteArchivo dtotransporte = new DTOTransporteArchivo();

                DTOArchivo archivo = new DTOArchivo();
                archivo.FechaCreacion = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                archivo.Id            = Guid.NewGuid().ToString();
                archivo.Tamano        = file.ContentLength.ToString();
                archivo.Nombre        = file.FileName;
                archivo.IdUsuario     = id_usuario;

                try
                {
                    // convert xmlstring to byte using ascii encoding
                    byte[] binario;
                    byte[] buffer = new byte[16 * 1024];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int read;
                        while ((read = file.InputStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        binario = ms.ToArray();
                    }
                    byte[] data = binario;
                    dtotransporte.Binario = data;
                    dtotransporte.Archivo = archivo;
                    ISynchronizationManager syn          = new SynchronizationManager();
                    Request request                      = new Request();
                    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                    request.Content     = json_serializer.Serialize(dtotransporte);
                    request.ContentType = "application/json";
                    request.Method      = "POST";
                    request.Url         = "http://192.168.160.98:10090/api/cac/v1/file/validator";

                    Response response = syn.PostRequest(request);
                    return(Json(response.TextResponse));
                }
                catch (Exception ex)
                {
                }
            }
            return(null);
        }
        public ActionResult UploadFileValidator(HttpPostedFileBase file = null)
        {
            string id_usuario = Request["user_id"];

            if (file != null)
            {
                DTOTransporteArchivo dtotransporte = new DTOTransporteArchivo();

                DTOArchivo archivo = new DTOArchivo();
                archivo.FechaCreacion = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                archivo.Id            = Guid.NewGuid().ToString();
                archivo.Tamano        = file.ContentLength.ToString();
                archivo.Nombre        = file.FileName;
                archivo.IdUsuario     = id_usuario;

                try
                {
                    // convert xmlstring to byte using ascii encoding
                    byte[] binario;
                    byte[] buffer = new byte[16 * 1024];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int read;
                        while ((read = file.InputStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        binario = ms.ToArray();
                    }
                    byte[] data      = null;
                    string directory = System.Web.Hosting.HostingEnvironment.MapPath($@"{Resources.TemporalServerFolder}");
                    string path_file = $@"{directory}/{file.FileName}";
                    if (Directory.Exists(directory) == false)
                    {
                        Directory.CreateDirectory(directory);
                    }
                    if (System.IO.File.Exists(path_file) == false)
                    {
                        // Create the file.
                        using (FileStream fs = System.IO.File.Create(path_file))
                        {
                            fs.Write(binario, 0, binario.Length);
                        }
                    }
                    else
                    {
                        System.IO.File.WriteAllBytes(path_file, binario);
                    }

                    FileInfo fileinfo         = new FileInfo(path_file);
                    string   compressfilename = ZipFile.CompressGZipStream(fileinfo);
                    if (compressfilename != null)
                    {
                        FileStream   fs = new FileStream(compressfilename, FileMode.Open, FileAccess.Read);
                        BinaryReader br = new BinaryReader(fs);
                        data = br.ReadBytes((int)fs.Length);
                        br.Close();
                        fs.Close();
                        System.IO.File.Delete(compressfilename);
                        System.IO.File.Delete(path_file);
                    }
                    dtotransporte.Binario = data;
                    dtotransporte.Archivo = archivo;
                    ISynchronizationManager syn          = new SynchronizationManager();
                    Request request                      = new Request();
                    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                    json_serializer.MaxJsonLength = 2147483647;
                    request.Content     = json_serializer.Serialize(dtotransporte);
                    request.ContentType = "application/json";
                    request.Method      = "POST";
                    request.Url         = $@"{url}/{Resources.FILE_VALIDATOR_POST}";

                    Response response = syn.PostRequest(request);
                    return(Json(response.TextResponse));
                }
                catch (Exception ex)
                {
                    return(Json(ex.Message));
                }
            }
            return(null);
        }