Monday, 8 December 2014

file upload and download in mvc

 public ActionResult Other(string id)
        {
            Other other = new Other();
            BOOfficeMaster bOOfficeMaster = bLOfficeMaster.GetOfficeMasterById(Convert.ToInt32(id));
            other.PSASOfficeCode = bOOfficeMaster.PSASOfficeCode;
            other.ProfitCenterCode = bOOfficeMaster.ProfitCenterCode;
            other.PFsubCode = bOOfficeMaster.PFsubCode;
            other.ESICSubcode = bOOfficeMaster.ESICSubcode;
            other.PasaraLicenceNumber = bOOfficeMaster.PasaraLicenceNumber;
            other.PlnFrom = Convert.ToDateTime(bOOfficeMaster.PlnFrom).ToShortDateString();
            other.PlnTo = Convert.ToDateTime(bOOfficeMaster.PlnTo).ToShortDateString();
            other.ShopEstablishmentLicenceNumber = bOOfficeMaster.ShopEstablishmentLicenceNumber;
            other.SelnFrom = Convert.ToDateTime(bOOfficeMaster.SelnFrom).ToShortDateString();
            other.SelnTo = Convert.ToDateTime(bOOfficeMaster.SelnTo).ToShortDateString();
            other.RentAgreement = bOOfficeMaster.RentAgreement;
            other.RaFrom = Convert.ToDateTime(bOOfficeMaster.RaFrom).ToShortDateString();
            other.RaTo = Convert.ToDateTime(bOOfficeMaster.RaTo).ToShortDateString();
            other.TrainingCertificateLicenceNumber = bOOfficeMaster.TrainingCertificateLicenceNumber;
            other.TclnFrom = Convert.ToDateTime(bOOfficeMaster.TclnFrom).ToShortDateString();
            other.TclnTo = Convert.ToDateTime(bOOfficeMaster.TclnTo).ToShortDateString();
            other.PasaraLicences = bOOfficeMaster.PasaraLicences;
            other.ShopEstablishmentLicences = bOOfficeMaster.ShopEstablishmentLicences;
            other.RentAgreements = bOOfficeMaster.RentAgreements;
            other.TrainingCertificates = bOOfficeMaster.TrainingCertificates;
            PSAS.Models.CustomerMaster.BusinessLayer.BLCustomerMaster oBLCustomerMaster = new Models.CustomerMaster.BusinessLayer.BLCustomerMaster();
            PSAS.Models.CustomerMaster.BusinessObject.BODocumentPathConfig oBODocumentPathConfig = null;
            oBODocumentPathConfig = oBLCustomerMaster.getDocumentPathConfig(91);
            //oBODocumentPathConfig = oBLCustomerMaster.getDocumentPathConfig(102);
            Folder oBODocumentUploaded = new Folder();
            DataTable DTfilePath = new DataTable();
            DTfilePath = oBODocumentUploaded.GetFileinfo();
            DataRow dtr = DTfilePath.NewRow();
            string FilePath = "";
            if (Directory.Exists(oBODocumentPathConfig.DestinationPath.Trim()))
            {
                if (Directory.Exists(Path.Combine(oBODocumentPathConfig.DestinationPath.Trim(), id.ToString())))
                {
                    foreach (var folderName in new DirectoryInfo(Path.Combine(oBODocumentPathConfig.DestinationPath.Trim(), id.ToString())).GetDirectories())
                    {
                        int i = folderName.GetFiles("*.*").Length;
                        if (i > 0)
                        {
                            foreach (var file in folderName.GetFiles())
                            {
                                FilePath = FilePath + "<a target='_parent' href='DownloadFiles?FilePath=" + Path.Combine(oBODocumentPathConfig.DestinationPath.Trim(), id.ToString()) + "\\" + folderName + "\\" + file + "&FileName=" + file + "&filetype=" + file.Extension + "' >Download</a>,";
                            }
                            FilePath = FilePath.Substring(0, FilePath.Length - 1);
                            dtr[folderName.ToString()] = FilePath;
                            FilePath = "";
                        }



                    }
                }
                DTfilePath.Rows.Add(dtr);
                ViewBag.datatable = DTfilePath;
            }
            return PartialView("Other", other);
        }
        public FileResult DownloadFiles(string FilePath, string FileName, string filetype)
        {

            String headerKey = "Content-Disposition";
            String headerValue = "attachment; filename=" + "" + FileName + "";
            Response.AppendHeader(headerKey, headerValue);
            Response.TransmitFile(FilePath);
            Response.End();
            return File(FilePath, filetype, FileName);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateOther(Other other)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bLOfficeMaster.UpdateOther(other);
                    string officeCode = other.PSASOfficeCode.ToString();
                    PSAS.Models.CustomerMaster.BusinessLayer.BLCustomerMaster oBLCustomerMaster = new Models.CustomerMaster.BusinessLayer.BLCustomerMaster();
                    PSAS.Models.CustomerMaster.BusinessObject.BODocumentPathConfig oBODocumentPathConfig = null;
                    //oBODocumentPathConfig = oBLCustomerMaster.getDocumentPathConfig(102);
                    oBODocumentPathConfig = oBLCustomerMaster.getDocumentPathConfig(91);
                    createfolder("PASARALICENCE", officeCode, oBODocumentPathConfig.DestinationPath);
                    createfolder("SHOPANDESTABLISHMENTLICENCE", officeCode, oBODocumentPathConfig.DestinationPath);
                    createfolder("RENTAGREEMENT", officeCode, oBODocumentPathConfig.DestinationPath);
                    createfolder("TRAININGCERTIFICATE", officeCode, oBODocumentPathConfig.DestinationPath);
                    if (other.PasaraLicencefu.ContentLength > 0)
                        other.PasaraLicencefu.SaveAs(Path.Combine(oBODocumentPathConfig.DestinationPath.Trim(), officeCode, "PASARALICENCE", other.PasaraLicencefu.FileName));
                    if (other.ShopEstablishmentLicencefu.ContentLength > 0)
                        other.ShopEstablishmentLicencefu.SaveAs(Path.Combine(oBODocumentPathConfig.DestinationPath.Trim(), officeCode, "SHOPANDESTABLISHMENTLICENCE", other.ShopEstablishmentLicencefu.FileName));
                    if (other.RentAgreementfu.ContentLength > 0)
                        other.RentAgreementfu.SaveAs(Path.Combine(oBODocumentPathConfig.DestinationPath.Trim(), officeCode, "RENTAGREEMENT", other.RentAgreementfu.FileName));
                    if (other.TrainingCertificatefu.ContentLength > 0)
                        other.TrainingCertificatefu.SaveAs(Path.Combine(oBODocumentPathConfig.DestinationPath.Trim(), officeCode, "TRAININGCERTIFICATE", other.TrainingCertificatefu.FileName));
                    TempData["success"] = "Data Updated Successfully.";
                 
                }
             
            }
            catch (Exception ex)
            {
                TempData["error"] = ex.Message.ToString();
            }
            return RedirectToAction("Details");
        }

//DestinationPath=C:\PSASMVC\AppDocument\PSASOFFICEDOCUMENT\(example)
   public void createfolder(string subFolderName, string officeCode, string DestinationPath)
        {
            if (!Directory.Exists(DestinationPath.Trim()))
            {
                DirectoryInfo di = Directory.CreateDirectory(DestinationPath.Trim());
            }
            if (!Directory.Exists(Path.Combine(DestinationPath.Trim(), officeCode)))
            {
                DirectoryInfo di = Directory.CreateDirectory(Path.Combine(DestinationPath.Trim(), officeCode));
            }
            if (!Directory.Exists(Path.Combine(DestinationPath.Trim(), officeCode, subFolderName)))
            {
                DirectoryInfo di = Directory.CreateDirectory(Path.Combine(DestinationPath.Trim(), officeCode, subFolderName));
            }
        }

DOM
 public class Folder
    {
        public DataTable GetFileinfo()
        {
            DataTable DTFileName = new DataTable();
            DTFileName.Columns.Add("PASARALICENCE", typeof(string));
            DTFileName.Columns.Add("SHOPANDESTABLISHMENTLICENCE", typeof(string));
            DTFileName.Columns.Add("RENTAGREEMENT", typeof(string));
            DTFileName.Columns.Add("TRAININGCERTIFICATE", typeof(string));
            return DTFileName;
        }
    }
    

No comments:

Post a Comment