コード例 #1
0
        public Tuple <string, string, bool> UploadLicenseColPdf(long licenseID, string fileDescription, IFormFile file)
        {
            Tuple <string, string, bool> oldFile = new Tuple <string, string, bool>("", "", false);
            string newFilePath = "";
            bool   hasFile     = false;

            //check then if there exist already one old file
            if (licenseID != 0)
            {
                oldFile = repository.GetLicenseColPath(licenseID);
            }

            if (file != null)
            {
                //Get the original file name
                var newFileName = Path.GetFileName(file.FileName);

                //Get the extension of the file
                string newFileExt = Path.GetExtension(file.FileName);

                //Get the new file path which is under wwwroot\licenseColPdf
                // Maybe a option to change the file name...
                newFilePath = Path.Combine(hostingEnv.WebRootPath, "licenseColPdf", newFileName);

                //Check if the file is not empty
                if (newFileName.Length > 0 && newFileExt.ToLower() == ".pdf")
                {
                    // If new file is different than local one, remove the old local PDF file.
                    if (newFilePath != oldFile.Item2)
                    {
                        // string fullPath = Request.MapPath("~/uploaded/" + file);
                        if (System.IO.File.Exists(oldFile.Item2))
                        {
                            System.IO.File.Delete(oldFile.Item2);
                        }
                    }

                    //Save the new file
                    using (var fileSteam = new FileStream(newFilePath, FileMode.Create))
                    {
                        file.CopyTo(fileSteam);
                    }
                    hasFile = true;
                }
                else
                {
                    return(new Tuple <string, string, bool>(oldFile.Item1, oldFile.Item2, oldFile.Item3));
                }
            }
            else
            {
                return(new Tuple <string, string, bool>(oldFile.Item1, oldFile.Item2, oldFile.Item3));
            }


            return(new Tuple <string, string, bool>(fileDescription, newFilePath, hasFile));
        }