Example #1
0
        private async Task <string> ProcessPathDocumentAsync(string FileName, string targetFilePathBackUp)
        {
            var parameters = new Common.GblParameter();

            parameters.SEARCH_KEY = "IMPORT_PRODUCT_FINISHED";

            var message = new Message();

            message.BusinessLogic = configuration.GetValue <string>("AppSettings:BusinessLogic:GblParameter");
            message.Connection    = configuration.GetValue <string>("ConnectionStrings:MEXPRESS_AC");
            message.Operation     = Operation.Get;
            using (var businessLgic = new ServiceManager())
            {
                message.MessageInfo = parameters.SerializeObject();
                var parameterResult = await businessLgic.DoWork(message);

                parameters = parameterResult.DeSerializeObject <Common.GblParameter>();
            }


            string targetFilePath = "";

            if (System.IO.File.Exists(targetFilePathBackUp))
            {
                targetFilePath = string.Format("{0}\\{1}", parameters.VALUE, FileName);
                string Path = string.Format("{0}", parameters.VALUE);

                if (!System.IO.File.Exists(targetFilePath))
                {
                    if (!Directory.Exists(Path))
                    {
                        Directory.CreateDirectory(Path);
                    }

                    Directory.Move(targetFilePathBackUp, targetFilePath);
                }
                else
                {
                    // System.IO.File.Delete(targetFilePath);
                    // if (!Directory.Exists(Path))
                    // {
                    //     Directory.CreateDirectory(Path);
                    // }
                    Directory.Move(targetFilePathBackUp, targetFilePath);

                    //throw new Exception("El archivo ingresado ya existe");
                }
            }

            targetFilePath = string.Format("{0}\\{1}", parameters.VALUE, FileName);
            return(targetFilePath);
        }
Example #2
0
        public async Task <IActionResult> ImportFile()
        {
            try
            {
                try
                {
                    if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
                    {
                        return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
                    }
                    var queryString = Request.Query;

                    /*
                     *  Sacamos la ruta de la carpeta para importar medios de contactos
                     */

                    var parameter = new Common.GblParameter();
                    parameter.SEARCH_KEY = "IMPORT_PRODUCT_PENDING";

                    var message = new Message();
                    message.BusinessLogic = configuration.GetValue <string>("AppSettings:BusinessLogic:GblParameter");
                    message.Connection    = configuration.GetValue <string>("ConnectionStrings:MEXPRESS_AC");
                    message.Operation     = Operation.Get;
                    using (var businessLgic = new ServiceManager())
                    {
                        message.MessageInfo = parameter.SerializeObject();
                        var parameterResult = await businessLgic.DoWork(message);

                        if (parameterResult.Status == Status.Failed)
                        {
                            return(BadRequest(parameterResult.Result));
                        }
                        parameter = parameterResult.DeSerializeObject <Common.GblParameter>();
                    }



                    // Used to accumulate all the form url encoded key value pairs in the
                    // request.

                    var    formAccumulator      = new KeyValueAccumulator();
                    var    nameFile             = String.Empty;
                    string targetFilePathBackUp = string.Empty;
                    string targetFilePath       = string.Empty;
                    var    boundary             = MultipartRequestHelper.GetBoundary(
                        Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse(Request.ContentType),
                        _defaultFormOptions.MultipartBoundaryLengthLimit);
                    var reader = new MultipartReader(boundary, HttpContext.Request.Body);

                    var section = await reader.ReadNextSectionAsync();

                    while (section != null)
                    {
                        Microsoft.Net.Http.Headers.ContentDispositionHeaderValue contentDisposition;
                        var hasContentDispositionHeader = Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                        if (hasContentDispositionHeader)
                        {
                            if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                            {
                                var date    = DateTime.Now;
                                var dateNow = date.Year.ToString() + "_" + date.Month.ToString() + "_" + date.Day.ToString() + "_" + date.Hour.ToString() + "_" + date.Minute.ToString() + "_" + date.Millisecond.ToString();
                                nameFile             = configuration.GetValue <string>("Files:FileName");
                                targetFilePathBackUp = string.Format("{0}{1}_{2}{3}", parameter.VALUE, nameFile, dateNow, ".xlsx");
                                nameFile             = string.Format("{0}_{1}{2}", nameFile, dateNow, ".xlsx");
                                if (!System.IO.File.Exists(targetFilePath))
                                {
                                    if (!Directory.Exists(parameter.VALUE))
                                    {
                                        Directory.CreateDirectory(parameter.VALUE);
                                    }
                                }


                                using (var targetStream = System.IO.File.Create(targetFilePathBackUp))
                                {
                                    await section.Body.CopyToAsync(targetStream);
                                }
                            }
                            else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                            {
                                var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                                var encoding = GetEncoding(section);
                                using (var streamReader = new StreamReader(
                                           section.Body,
                                           encoding,
                                           detectEncodingFromByteOrderMarks: true,
                                           bufferSize: 1024,
                                           leaveOpen: true))
                                {
                                    // The value length limit is enforced by MultipartBodyLengthLimit
                                    var value = await streamReader.ReadToEndAsync();

                                    if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                                    {
                                        value = String.Empty;
                                    }
                                    formAccumulator.Append(key.Value, value);

                                    if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                                    {
                                        throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                                    }
                                }
                            }
                        }

                        // Drains any remaining section body that has not been consumed and
                        // reads the headers for the next section.
                        section = await reader.ReadNextSectionAsync();
                    }

                    var model = new Common.Import_Product();
                    model = GetHeaderPk(formAccumulator.GetResults());
                    model.Creation_User = (queryString["User"]).ToString();
                    model.File_Path     = ProcessPathDocumentAsync(nameFile, targetFilePathBackUp).Result;

                    var formValueProvider = new FormValueProvider(
                        BindingSource.Form,
                        new FormCollection(formAccumulator.GetResults()),
                        CultureInfo.CurrentCulture);

                    var bindingSuccessful = await TryUpdateModelAsync(model, prefix : "",
                                                                      valueProvider : formValueProvider);


                    message.BusinessLogic = configuration.GetValue <string>("AppSettings:BusinessLogic:Gbl_Wrk_Agreement_Detail");
                    message.Connection    = configuration.GetValue <string>("ConnectionStrings:MEXPRESS_AC");
                    message.Operation     = Operation.Save;
                    using (var businessLgic = new ServiceManager())
                    {
                        message.MessageInfo = model.SerializeObject();
                        var result = await businessLgic.DoWork(message);

                        if (result.Status == Status.Failed)
                        {
                            return(BadRequest(result.Result));
                        }
                        var list = result.DeSerializeObject <IEnumerable <Common.Import_Product> >();

                        return(Ok(list));
                    }
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }