Beispiel #1
0
        protected void ProcessFileUploads(ProductEditAdminViewModel viewModel, StoreFront storeFront)
        {
            //todo: temporary file process

            string virtualFolder = storeFront.CatalogProductContentVirtualDirectoryToMap(Request.ApplicationPath);
            string fileFolder    = Server.MapPath(virtualFolder);

            if (!System.IO.Directory.Exists(fileFolder))
            {
                System.IO.Directory.CreateDirectory(fileFolder);
            }


            HttpPostedFileBase imageFile = Request.Files["ImageName_File"];

            if (imageFile != null && imageFile.ContentLength != 0)
            {
                string newFileName = imageFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["ImageName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_Image." + imageFile.FileName.FileExtension();
                }

                try
                {
                    imageFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product image file '" + imageFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.ImageName = newFileName;
                AddUserMessage("Image Uploaded!", "Product Image '" + imageFile.FileName.ToHtml() + "' " + imageFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase digitalDownloadFile = Request.Files["DigitalDownloadFileName_File"];

            if (digitalDownloadFile != null && digitalDownloadFile.ContentLength != 0)
            {
                string digitalDownloadVirtualFolder = storeFront.ProductDigitalDownloadVirtualDirectoryToMap(Request.ApplicationPath);
                string digitalDownloadFileFolder    = Server.MapPath(digitalDownloadVirtualFolder);
                if (!System.IO.Directory.Exists(digitalDownloadFileFolder))
                {
                    System.IO.Directory.CreateDirectory(digitalDownloadFileFolder);
                }

                string newFileName = digitalDownloadFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["DigitalDownloadFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_DigitalDownload." + digitalDownloadFile.FileName.FileExtension();
                }

                try
                {
                    digitalDownloadFile.SaveAs(digitalDownloadFileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product digital download file '" + digitalDownloadFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.DigitalDownloadFileName = newFileName;
                AddUserMessage("Digital Download Uploaded!", "Product Digital Download File '" + digitalDownloadFile.FileName.ToHtml() + "' " + digitalDownloadFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase sampleAudioFile = Request.Files["SampleAudioFileName_File"];

            if (sampleAudioFile != null && sampleAudioFile.ContentLength != 0)
            {
                string newFileName = digitalDownloadFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["SampleAudioFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_SampleAudio." + sampleAudioFile.FileName.FileExtension();
                }

                try
                {
                    sampleAudioFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product sample audio file '" + sampleAudioFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.SampleAudioFileName = newFileName;
                AddUserMessage("Sample Audio Uploaded!", "Product Sample Audio File '" + sampleAudioFile.FileName.ToHtml() + "' " + sampleAudioFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase sampleDownloadFile = Request.Files["SampleDownloadFileName_File"];

            if (sampleDownloadFile != null && sampleDownloadFile.ContentLength != 0)
            {
                string newFileName = sampleDownloadFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["SampleAudioFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_SampleDownload." + sampleDownloadFile.FileName.FileExtension();
                }

                try
                {
                    sampleDownloadFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product sample download file '" + sampleDownloadFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.SampleDownloadFileName = newFileName;
                AddUserMessage("Sample Download File Uploaded!", "Product Sample Download File '" + sampleDownloadFile.FileName.ToHtml() + "' " + sampleDownloadFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase sampleImageFile = Request.Files["SampleImageFileName_File"];

            if (sampleImageFile != null && sampleImageFile.ContentLength != 0)
            {
                string newFileName = sampleImageFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["SampleAudioFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_SampleImage." + sampleImageFile.FileName.FileExtension();
                }

                try
                {
                    sampleImageFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product sample image file '" + sampleImageFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.SampleImageFileName = newFileName;
                AddUserMessage("Sample Image File Uploaded!", "Sample Image File '" + sampleImageFile.FileName.ToHtml() + "' " + sampleImageFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }
        }
        protected void ProcessFileUploads(ProductEditAdminViewModel viewModel, StoreFront storeFront)
        {
            //todo: temporary file process

            string virtualFolder = storeFront.CatalogProductContentVirtualDirectoryToMap(Request.ApplicationPath);
            string fileFolder = Server.MapPath(virtualFolder);
            if (!System.IO.Directory.Exists(fileFolder))
            {
                System.IO.Directory.CreateDirectory(fileFolder);
            }

            HttpPostedFileBase imageFile = Request.Files["ImageName_File"];
            if (imageFile != null && imageFile.ContentLength != 0)
            {
                string newFileName = imageFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["ImageName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_Image." + imageFile.FileName.FileExtension();
                }

                try
                {
                    imageFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product image file '" + imageFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.ImageName = newFileName;
                AddUserMessage("Image Uploaded!", "Product Image '" + imageFile.FileName.ToHtml() + "' " + imageFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase digitalDownloadFile = Request.Files["DigitalDownloadFileName_File"];
            if (digitalDownloadFile != null && digitalDownloadFile.ContentLength != 0)
            {
                string digitalDownloadVirtualFolder = storeFront.ProductDigitalDownloadVirtualDirectoryToMap(Request.ApplicationPath);
                string digitalDownloadFileFolder = Server.MapPath(digitalDownloadVirtualFolder);
                if (!System.IO.Directory.Exists(digitalDownloadFileFolder))
                {
                    System.IO.Directory.CreateDirectory(digitalDownloadFileFolder);
                }

                string newFileName = digitalDownloadFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["DigitalDownloadFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_DigitalDownload." + digitalDownloadFile.FileName.FileExtension();
                }

                try
                {
                    digitalDownloadFile.SaveAs(digitalDownloadFileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product digital download file '" + digitalDownloadFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.DigitalDownloadFileName = newFileName;
                AddUserMessage("Digital Download Uploaded!", "Product Digital Download File '" + digitalDownloadFile.FileName.ToHtml() + "' " + digitalDownloadFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase sampleAudioFile = Request.Files["SampleAudioFileName_File"];
            if (sampleAudioFile != null && sampleAudioFile.ContentLength != 0)
            {
                string newFileName = digitalDownloadFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["SampleAudioFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_SampleAudio." + sampleAudioFile.FileName.FileExtension();
                }

                try
                {
                    sampleAudioFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product sample audio file '" + sampleAudioFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.SampleAudioFileName = newFileName;
                AddUserMessage("Sample Audio Uploaded!", "Product Sample Audio File '" + sampleAudioFile.FileName.ToHtml() + "' " + sampleAudioFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase sampleDownloadFile = Request.Files["SampleDownloadFileName_File"];
            if (sampleDownloadFile != null && sampleDownloadFile.ContentLength != 0)
            {
                string newFileName = sampleDownloadFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["SampleAudioFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_SampleDownload." + sampleDownloadFile.FileName.FileExtension();
                }

                try
                {
                    sampleDownloadFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product sample download file '" + sampleDownloadFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.SampleDownloadFileName = newFileName;
                AddUserMessage("Sample Download File Uploaded!", "Product Sample Download File '" + sampleDownloadFile.FileName.ToHtml() + "' " + sampleDownloadFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }

            HttpPostedFileBase sampleImageFile = Request.Files["SampleImageFileName_File"];
            if (sampleImageFile != null && sampleImageFile.ContentLength != 0)
            {
                string newFileName = sampleImageFile.FileNameNoPath();
                if (!string.IsNullOrEmpty(Request.Form["SampleAudioFileName_ChangeFileName"]))
                {
                    newFileName = viewModel.UrlName + "_SampleImage." + sampleImageFile.FileName.FileExtension();
                }

                try
                {
                    sampleImageFile.SaveAs(fileFolder + "\\" + newFileName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error saving product sample image file '" + sampleImageFile.FileName + "' as '" + newFileName + "'", ex);
                }

                viewModel.SampleImageFileName = newFileName;
                AddUserMessage("Sample Image File Uploaded!", "Sample Image File '" + sampleImageFile.FileName.ToHtml() + "' " + sampleImageFile.ContentLength.ToByteString() + " was saved as '" + newFileName + "'", UserMessageType.Success);
            }
        }
Beispiel #3
0
        public static IEnumerable <SelectListItem> ProductFileListForModel(this HtmlHelper htmlHelper, string searchPattern = "*.*", bool filterForImages = false, bool filterForAudio = false)
        {
            string selectedFileName = (htmlHelper.ViewData.Model as string ?? "").ToLower();

            StoreFront storeFront = htmlHelper.CurrentStoreFront(true);
            Client     client     = storeFront.Client;

            HttpContextBase http = htmlHelper.ViewContext.RequestContext.HttpContext;

            string storeFrontVirtualPath = storeFront.CatalogProductContentVirtualDirectoryToMap(http.Request.ApplicationPath);
            string storeFrontFolderPath  = http.Server.MapPath(storeFrontVirtualPath);
            List <SelectListItem> storeFrontListItems = new List <SelectListItem>();

            if (System.IO.Directory.Exists(storeFrontFolderPath))
            {
                DirectoryInfo   storeFrontFolder = new System.IO.DirectoryInfo(storeFrontFolderPath);
                List <FileInfo> storeFrontFiles  = storeFrontFolder.GetFiles(searchPattern).ToList();
                if (filterForImages && filterForAudio)
                {
                    storeFrontFiles = storeFrontFiles.Where(f => f.Name.FileExtensionIsImage() || f.Name.FileExtensionIsAudio()).ToList();
                }
                else if (filterForImages)
                {
                    storeFrontFiles = storeFrontFiles.Where(f => f.Name.FileExtensionIsImage()).ToList();
                }
                else if (filterForAudio)
                {
                    storeFrontFiles = storeFrontFiles.Where(f => f.Name.FileExtensionIsAudio()).ToList();
                }

                SelectListGroup storeFrontGroup = new SelectListGroup()
                {
                    Name = "Store Front"
                };
                storeFrontListItems = storeFrontFiles.Select(fil =>
                                                             new SelectListItem()
                {
                    Value    = fil.Name,
                    Text     = fil.Name + (fil.Name.ToLower() == selectedFileName ? " [SELECTED]" : "") + " " + fil.Length.ToByteString(),
                    Selected = fil.Name.ToLower() == selectedFileName,
                    Group    = storeFrontGroup
                }).ToList();
            }

            string clientVirtualPath = client.CatalogProductContentVirtualDirectoryToMap(http.Request.ApplicationPath);
            string clientFolderPath  = http.Server.MapPath(clientVirtualPath);
            List <SelectListItem> clientListItems = new List <SelectListItem>();

            if (System.IO.Directory.Exists(clientFolderPath))
            {
                DirectoryInfo   clientFolder = new System.IO.DirectoryInfo(clientFolderPath);
                List <FileInfo> clientFiles  = clientFolder.EnumerateFiles(searchPattern).ToList();
                if (filterForAudio && filterForImages)
                {
                    clientFiles = clientFiles.Where(f => f.Name.FileExtensionIsAudio() || f.Name.FileExtensionIsImage()).ToList();
                }
                else if (filterForImages)
                {
                    clientFiles = clientFiles.Where(f => f.Name.FileExtensionIsImage()).ToList();
                }
                else if (filterForAudio)
                {
                    clientFiles = clientFiles.Where(f => f.Name.FileExtensionIsAudio()).ToList();
                }

                SelectListGroup clientGroup = new SelectListGroup()
                {
                    Name = "Client"
                };
                clientListItems = clientFiles.Select(fil =>
                                                     new SelectListItem()
                {
                    Value    = fil.Name,
                    Text     = fil.Name + (fil.Name.ToLower() == selectedFileName ? " [SELECTED]" : "") + " " + fil.Length.ToByteString(),
                    Selected = fil.Name.ToLower() == selectedFileName,
                    Group    = clientGroup
                }).ToList();
            }

            string serverVirtualPath = "~/Content/Server/CatalogContent/Products";
            string serverFolderPath  = http.Server.MapPath(serverVirtualPath);
            List <SelectListItem> serverListItems = new List <SelectListItem>();

            if (System.IO.Directory.Exists(serverFolderPath))
            {
                DirectoryInfo   serverFolder = new System.IO.DirectoryInfo(serverFolderPath);
                List <FileInfo> serverFiles  = serverFolder.EnumerateFiles(searchPattern).ToList();
                if (filterForAudio && filterForImages)
                {
                    serverFiles = serverFiles.Where(f => f.Name.FileExtensionIsAudio() || f.Name.FileExtensionIsImage()).ToList();
                }
                else if (filterForImages)
                {
                    serverFiles = serverFiles.Where(f => f.Name.FileExtensionIsImage()).ToList();
                }
                else if (filterForAudio)
                {
                    serverFiles = serverFiles.Where(f => f.Name.FileExtensionIsAudio()).ToList();
                }

                SelectListGroup serverGroup = new SelectListGroup()
                {
                    Name = "GStore Files"
                };
                serverListItems = serverFiles.Select(fil =>
                                                     new SelectListItem()
                {
                    Value    = fil.Name,
                    Text     = fil.Name + (fil.Name.ToLower() == selectedFileName ? " [SELECTED]" : "") + " " + fil.Length.ToByteString(),
                    Selected = fil.Name.ToLower() == selectedFileName,
                    Group    = serverGroup
                }).ToList();
            }

            return(storeFrontListItems.Concat(clientListItems).Concat(serverListItems));
        }