// Upload entire file
        private void UploadWholeFile(HttpContext context, List <Resource_FilesStatus> statuses)
        {
            for (int i = 0; i < context.Request.Files.Count; i++)
            {
                var file = context.Request.Files[i];

                if (file != null && !String.IsNullOrEmpty(file.FileName) && file.ContentLength > 0)
                {
                    //验证后缀名是否符合
                    bool Valid = FileSystemUtils.CheckValidFileName(file.FileName);
                    if (Valid)
                    {
                        DNNGo_DNNGalleryPro_Files PhotoItem = new DNNGo_DNNGalleryPro_Files();

                        PhotoItem.ModuleId = WebHelper.GetIntParam(context.Request, "ModuleId", 0);
                        PhotoItem.PortalId = WebHelper.GetIntParam(context.Request, "PortalId", 0);


                        PhotoItem.FileName = file.FileName;
                        PhotoItem.FileSize = file.ContentLength / 1024;
                        PhotoItem.FileMate = WebHelper.leftx(FileSystemUtils.GetContentType(Path.GetExtension(PhotoItem.FileName).Replace(".", "")), 30);

                        PhotoItem.FileExtension = System.IO.Path.GetExtension(PhotoItem.FileName).Replace(".", "");
                        PhotoItem.Name          = System.IO.Path.GetFileName(file.FileName).Replace(Path.GetExtension(PhotoItem.FileName), "");
                        PhotoItem.Status        = (Int32)EnumFileStatus.Approved;

                        try
                        {
                            if (("png,gif,jpg,jpeg,bmp").IndexOf(PhotoItem.FileExtension) >= 0)
                            {
                                //图片的流
                                Image image = Image.FromStream(file.InputStream);
                                PhotoItem.ImageWidth  = image.Width;
                                PhotoItem.ImageHeight = image.Height;

                                PhotoItem.Exif = Common.Serialize <EXIFMetaData.Metadata>(new EXIFMetaData().GetEXIFMetaData(image));
                            }
                        }
                        catch
                        {
                        }

                        PhotoItem.LastTime = xUserTime.UtcTime();
                        PhotoItem.LastIP   = WebHelper.UserHost;
                        PhotoItem.LastUser = UserInfo.UserID;


                        //将文件存储的路径整理好
                        String fileName = FileSystemUtils.HandleFileName(System.IO.Path.GetFileName(file.FileName).Replace("." + PhotoItem.FileExtension, ""));  //文件名称
                        String WebPath  = String.Format("DNNGalleryPro/uploads/{0}/{1}/{2}/", PhotoItem.LastTime.Year, PhotoItem.LastTime.Month, PhotoItem.LastTime.Day);
                        //检测文件存储路径是否有相关的文件
                        System.IO.FileInfo fInfo = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(String.Format("{0}{1}{2}.{3}", PortalSettings.HomeDirectory, WebPath, fileName, PhotoItem.FileExtension)));

                        //检测文件夹是否存在
                        if (!System.IO.Directory.Exists(fInfo.Directory.FullName))
                        {
                            System.IO.Directory.CreateDirectory(fInfo.Directory.FullName);
                        }
                        else
                        {
                            Int32 j = 1;
                            while (fInfo.Exists)
                            {
                                //文件已经存在了
                                fileName = String.Format("{0}_{1}", FileSystemUtils.HandleFileName(PhotoItem.Name), j);
                                fInfo    = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(String.Format("{0}{1}{2}.{3}", PortalSettings.HomeDirectory, WebPath, fileName, PhotoItem.FileExtension)));
                                j++;
                            }
                        }

                        PhotoItem.FilePath = String.Format("{0}{1}.{2}", WebPath, fileName, PhotoItem.FileExtension);
                        PhotoItem.FileName = String.Format("{0}.{1}", fileName, PhotoItem.FileExtension);
                        try
                        {
                            if (!fInfo.Directory.Exists)
                            {
                                fInfo.Directory.Create();

                                // FileSystemUtils.AddFolder(PortalSettings, String.Format("{0}DNNGo_PhotoAlbums/{0}/{1}/"), String.Format("{0}DNNGo_PhotoAlbums/{0}/{1}/"), (int)DotNetNuke.Services.FileSystem.FolderController.StorageLocationTypes.InsecureFileSystem);
                            }

                            //构造指定存储路径
                            //file.SaveAs(fInfo.FullName);
                            ChangeImageSize(file, fInfo.FullName);
                            //FileSystemUtils.AddFile(PhotoItem.FileName, PhotoItem.PortalId, String.Format("DNNGo_PhotoAlbums\\{0}\\{1}\\", PhotoItem.ModuleId, PhotoItem.AlbumID), PortalSettings.HomeDirectoryMapPath, PhotoItem.FileMeta);
                        }
                        catch (Exception ex)
                        {
                        }

                        //给上传的相片设置初始的顺序
                        QueryParam qp = new QueryParam();
                        qp.ReturnFields = qp.Orderfld = DNNGo_DNNGalleryPro_Files._.Sort;
                        qp.OrderType    = 1;
                        qp.Where.Add(new SearchParam(DNNGo_DNNGalleryPro_Files._.PortalId, PhotoItem.PortalId, SearchType.Equal));
                        PhotoItem.Sort = Convert.ToInt32(DNNGo_DNNGalleryPro_Files.FindScalar(qp)) + 2;
                        Int32 PhotoId = PhotoItem.Insert();



                        statuses.Add(new Resource_FilesStatus(PhotoItem, PortalSettings, ModulePath));
                    }
                    else
                    {
                        throw new HttpRequestValidationException("Following file was imported/uploaded, but is not an authorized filetype: " + file.FileName);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 导入图片到媒体库
        /// </summary>
        /// <param name="PictureUrl">图片地址</param>
        /// <returns></returns>
        public String ImportPicture(String PictureUrl)
        {
            String Picture = "";

            if (!String.IsNullOrEmpty(PictureUrl))
            {
                //查看该图片是否已经存在过
                KeyValueEntity PictureTemp = ImportPictureList.Find(r1 => r1.Key == PictureUrl);
                if (PictureTemp != null && !String.IsNullOrEmpty(PictureTemp.Key))
                {
                    Picture = PictureTemp.Value.ToString();
                }
                else
                {
                    DNNGo_DNNGalleryPro_Files PhotoItem = new DNNGo_DNNGalleryPro_Files();

                    //将图片的URL转换为相应的文件名,文件后缀等内容
                    PhotoItem.FileName = System.IO.Path.GetFileName(PictureUrl);//文件名 “Default.aspx”

                    if (!String.IsNullOrEmpty(PhotoItem.FileName) && PhotoItem.FileName.IndexOf(".") > 0)
                    {
                        PhotoItem.FileExtension = System.IO.Path.GetExtension(PhotoItem.FileName).Replace(".", "");
                        PhotoItem.Name          = PhotoItem.FileName.Replace(System.IO.Path.GetExtension(PictureUrl), "");

                        //判断哪些文件需要被下载
                        if (NeedExtension(PhotoItem.FileExtension))
                        {
                            String   FullFile = String.Format("{0}DNNGalleryPro\\uploads\\{1}\\{2}\\{3}\\{4}", DNNGalleryPro_PortalSettings.HomeDirectoryMapPath, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PhotoItem.FileName);
                            FileInfo file     = new FileInfo(FullFile);

                            int ExistsCount = 1;
                            //如果有重复的文件,需要改变文件名
                            while (file.Exists)
                            {
                                PhotoItem.FileName = String.Format("{0}_{1}.{2}", PhotoItem.Name, ExistsCount, PhotoItem.FileExtension);
                                FullFile           = String.Format("{0}DNNGalleryPro\\uploads\\{1}\\{2}\\{3}\\{4}", DNNGalleryPro_PortalSettings.HomeDirectoryMapPath, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PhotoItem.FileName);
                                file = new FileInfo(FullFile);
                                ExistsCount++;
                            }

                            try
                            {
                                if (!file.Directory.Exists)
                                {
                                    file.Directory.Create();
                                }

                                //下载图片
                                WebClientX web = new WebClientX();
                                web.DownloadFile(new Uri(PictureUrl), FullFile);

                                file = new FileInfo(FullFile);
                                if (file.Exists)
                                {
                                    PhotoItem.ModuleId = ModuleID;
                                    PhotoItem.PortalId = DNNGalleryPro_PortalSettings.PortalId;
                                    PhotoItem.FileSize = Convert.ToInt32(file.Length / 1024);
                                    PhotoItem.FileMate = FileSystemUtils.GetContentType(Path.GetExtension(PhotoItem.FileName).Replace(".", ""));
                                    PhotoItem.Status   = (Int32)EnumFileStatus.Approved;
                                    PhotoItem.FilePath = String.Format("DNNGalleryPro/uploads/{0}/{1}/{2}/{3}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PhotoItem.FileName);


                                    try
                                    {
                                        if (("png,gif,jpg,jpeg,bmp").IndexOf(PhotoItem.FileExtension) >= 0)
                                        {
                                            //图片的流
                                            System.Drawing.Image image = System.Drawing.Image.FromFile(file.FullName);
                                            PhotoItem.ImageWidth  = image.Width;
                                            PhotoItem.ImageHeight = image.Height;

                                            PhotoItem.Exif = Common.Serialize <EXIFMetaData.Metadata>(new EXIFMetaData().GetEXIFMetaData(image));
                                        }
                                    }
                                    catch
                                    {
                                    }

                                    PhotoItem.LastTime = DateTime.Now;
                                    PhotoItem.LastIP   = WebHelper.UserHost;
                                    //PhotoItem.LastUser = DNNGalleryPro_PortalSettings.UserInfo.UserID;
                                    PhotoItem.ID = PhotoItem.Insert();

                                    //返回图片的路径
                                    Picture = String.Format("MediaID={0}", PhotoItem.ID);
                                    ImportPictureList.Add(new KeyValueEntity(PictureUrl, Picture));
                                }
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        else
                        {
                            Picture = PictureUrl;
                        }
                    }
                    else
                    {
                        Picture = PictureUrl;
                    }
                }
            }


            return(Picture);
        }
Beispiel #3
0
        /// <summary>
        /// 媒体库文件下载
        /// </summary>
        public void FileDownLoads()
        {
            Int32  SliderID    = WebHelper.GetIntParam(Request, "SliderID", 0);
            Int32  LayerID     = WebHelper.GetIntParam(Request, "LayerID", 0);
            String SettingName = WebHelper.GetStringParam(Request, "SettingName", "");

            if (!String.IsNullOrEmpty(SettingName))
            {
                String SettingValue = String.Empty;

                DNNGo_DNNGalleryPro_Slider SliderItem = new DNNGo_DNNGalleryPro_Slider();
                if (LayerID > 0)
                {
                    DNNGo_DNNGalleryPro_Layer LayerItem = DNNGo_DNNGalleryPro_Layer.FindByKeyForEdit(LayerID);
                    if (LayerItem != null && LayerItem.ID > 0)
                    {
                        SliderItem = DNNGo_DNNGalleryPro_Slider.FindByKeyForEdit(SliderID);
                        if (SliderItem != null && SliderItem.ID > 0)
                        {
                            //背景下载技术
                            SliderItem.Clicks += 1;
                            SliderItem.Update();
                        }
                        //层下载技术
                        LayerItem.Clicks += 1;
                        LayerItem.Update();
                        //取出层中的下载链接
                        SettingValue = ViewLayerSettingT <String>(LayerItem, SettingName, "");
                    }
                    else
                    {
                        //没有找到文件记录
                    }
                }
                else if (SliderID > 0)
                {
                    SliderItem = DNNGo_DNNGalleryPro_Slider.FindByKeyForEdit(SliderID);
                    if (SliderItem != null && SliderItem.ID > 0)
                    {
                        SliderItem.Clicks += 1;
                        SliderItem.Update();
                        //取出背景中的下载链接
                        SettingValue = ViewSliderSettingT <String>(SliderItem, SettingName, "");
                    }
                }

                if (!String.IsNullOrEmpty(SettingValue))
                {
                    String DownLoadUrl = HttpUtility.UrlDecode(ViewLinkUrl(SettingValue, false));
                    if (!String.IsNullOrEmpty(DownLoadUrl))
                    {
                        //记录下载信息到日志表里面***暂时忽略


                        if (SettingValue.IndexOf("MediaID=", StringComparison.CurrentCultureIgnoreCase) == 0)
                        {
                            String ServerPath = MapPath(DownLoadUrl);

                            if (false)
                            {
                                FileSystemUtils.DownloadFile(ServerPath, Path.GetFileName(ServerPath));//下载媒体库中的文件
                            }
                            else
                            {
                                Response.Redirect(DownLoadUrl);
                            }
                        }
                        else
                        {
                            Response.Redirect(DownLoadUrl);//除了媒体库中的文件,其他一律跳转
                        }
                    }
                    else
                    {
                        //下载链接不存在
                    }
                }
                else
                {
                    //没有取到下载链接
                }
            }
            else
            {
                //没有找到文件参数
            }
        }