Ejemplo n.º 1
0
        public ActionResult Upload(HttpPostedFileBase fileData, string businessDate)
        {
            string zipDirectory = string.Empty;

            try
            {
                if (fileData == null)
                {
                    return Content("失败:上传文件不存在!");
                 }

                string uploadDirectory = Server.MapPath(NMBConfig.UploadDirectory);

                BLLFileProcess bllFileProcess = new BLLFileProcess();
                NMB.Model.FileInfo fileInfo;

                string extenName = Path.GetExtension(fileData.FileName).ToLower();
                if (extenName.Contains("zip"))
                {
                    zipDirectory = Path.Combine(uploadDirectory, "TempZip");
                    if (!Directory.Exists(zipDirectory))
                    {
                        Directory.CreateDirectory(zipDirectory);
                    }

                    foreach (string file in Directory.GetFiles(zipDirectory))
                    {
                        System.IO.File.Delete(file);
                    }

                    string zipFileName = Path.Combine(zipDirectory, fileData.FileName);
                    fileData.SaveAs(zipFileName);

                    using (ZipFile zipFile = ZipFile.Read(zipFileName))
                    {
                        foreach (ZipEntry entry in zipFile)
                        {
                            entry.Extract(zipDirectory);
                        }
                    }

                    System.IO.File.Delete(zipFileName);

                    string unZipFile = Directory.GetFiles(zipDirectory)[0];
                    string unZipFileName = Path.GetFileName(unZipFile);

                    if (bllFileProcess.IsCanUpload(businessDate, unZipFileName, out fileInfo))
                    {
                        if (fileInfo.Type != FileInfoType.NoMasterBase && fileInfo.Type != FileInfoType.BillBase)
                        {
                            uploadDirectory = Path.Combine(uploadDirectory, businessDate);
                        }

                        if (!Directory.Exists(uploadDirectory))
                        {
                            Directory.CreateDirectory(uploadDirectory);
                        }

                        string saveFileName = Path.Combine(uploadDirectory, unZipFileName);

                        //File.Copy(unZipFile, saveFileName, true);

                        FTP ftp = new FTP(new Uri(NMBConfig.FtpURL), NMBConfig.FtpUserName, NMBConfig.FtpUserPass);

                        string projectDirectory = "NoMasterAndBill";

                        if (!ftp.DirectoryExist(projectDirectory))
                        {
                            ftp.MakeDirectory(projectDirectory);
                        }

                        ftp.GotoDirectory(projectDirectory);

                        if (!ftp.DirectoryExist(businessDate))
                        {
                            ftp.MakeDirectory(businessDate);
                        }

                        ftp.GotoDirectory(businessDate);
                        ftp.UploadFileBigFile(unZipFile, true);

                        bllFileProcess.FileUploadRecord(businessDate, fileInfo.Id, saveFileName);

                        return Content("完成");
                    }
                    else
                    {
                        return Content("失败:上传压缩包内文件名不符合要求!");
                    }
                }
                else if (bllFileProcess.IsCanUpload(businessDate, fileData.FileName, out fileInfo))
                {
                    if (fileInfo.Type != FileInfoType.NoMasterBase && fileInfo.Type != FileInfoType.BillBase)
                    {
                        uploadDirectory = Path.Combine(uploadDirectory, businessDate);
                    }

                    if (!Directory.Exists(uploadDirectory))
                    {
                        Directory.CreateDirectory(uploadDirectory);
                    }

                    string saveFileName = Path.Combine(uploadDirectory, fileData.FileName);

                    fileData.SaveAs(saveFileName);

                    if (Path.GetExtension(saveFileName).ToLower() == ".xls")
                    {
                        using (ExcelProcess excelProcess = new ExcelProcess(saveFileName))
                        {
                            excelProcess.SaveAs(saveFileName);
                            excelProcess.Dispose();
                        }
                    }

                    bllFileProcess.FileUploadRecord(businessDate, fileInfo.Id, saveFileName);

                    return Content("完成");
                }
                else
                {
                    return Content("失败:上传压缩包内文件名不符合要求!");
                }
            }
            catch (Exception ex)
            {
                Tools.LogWrite(ex.ToString());
                return Content("异常:" + ex.Message);
            }
            finally
            {
                if (Directory.Exists(zipDirectory))
                {
                    foreach (string file in Directory.GetFiles(zipDirectory))
                    {
                        System.IO.File.Delete(file);
                    }
                }
            }
        }