Exemplo n.º 1
0
        public ActionResult UploadFile(UploadFileModel model, int Type, HttpPostedFileBase file)
        {
            //if (DevExpressHelper.IsCallback)
            //{
            if (ModelState.IsValid && Type > 0)
            {
                byte[] fileBytes  = new byte[1];
                string outputFile = " //payment.xml";
                if (Request.Files.Count > 0)
                {
                    var f = Request.Files[0];

                    if (f != null && file.ContentLength > 0)
                    {
                        if (!Directory.Exists(Server.MapPath("~/Content/Uploads")))
                        {
                            Directory.CreateDirectory(Server.MapPath("~/Content/Uploads"));
                        }
                        var fileName = Path.GetFileName(file.FileName);
                        var path     = Path.Combine(Server.MapPath("~/Content/Uploads"), fileName);
                        var savePath = Server.MapPath("~/Content/Output");
                        file.SaveAs(path);
                        var ext = Path.GetExtension(path);
                        if (ext == ".xls")
                        {
                            path = Convert(path, true);
                            if ((enPostType)Type == enPostType.Invoice_12_5_WithAddress)
                            {
                                outputFile = " //payment.xml";
                                _converter = new ConvertInvoiceVoucherToXmlService();
                            }
                            else if ((enPostType)Type == enPostType.Purchase)
                            {
                                outputFile = " //purchase.xml";
                                _converter = new ConvertPurchaseRegisterToXmlService();
                            }
                            _provider = new XmlProvider(_converter, model, path, savePath);
                            _provider.ConvertToXml();

                            ViewData["Msg"] = "File uploaded succesfully. Xml file generated";

                            fileBytes = System.IO.File.ReadAllBytes(savePath + outputFile);
                        }
                        else
                        {
                            ViewData["MsgError"] = "File conversion failed. File type not excel. Check file extension!!";
                        }
                    }
                }

                //}
                if (fileBytes.Length > 10)
                {
                    return(File(fileBytes, "application/xml", outputFile.Substring(3)));
                }
                else
                {
                    ViewData["type"] = EnumHelper.ToList(typeof(enPostType));
                    return(View(model));
                }
            }

            else
            {
                ModelState.AddModelError("", "Upload Type not specified!");
                ViewData["type"] = EnumHelper.ToList(typeof(enPostType));
                return(View(model));
            }
        }