Exemplo n.º 1
0
        // 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)
                {
                    //To verify that if the suffix name of the uploaded files meets the DNN HOST requirements
                    Boolean retValue = FileSystemUtils.CheckValidFileName(file.FileName);
                    if (retValue)
                    {
                        Playngo_ClientZone_Files PhotoItem = new Playngo_ClientZone_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;
                        PhotoItem.Extension1 = Visibility;

                        String SaveDirPath = "uploads";

                        if (Visibility == 1)
                        {
                            SaveDirPath = "downloads";
                        }

                        //将文件存储的路径整理好
                        String fileName = System.IO.Path.GetFileName(file.FileName).Replace("." + PhotoItem.FileExtension, ""); //文件名称
                        String WebPath  = String.Format("ClientZone/{0}/{1}/{2}/{3}/", SaveDirPath, DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day);
                        //检测文件存储路径是否有相关的文件
                        FileInfo fInfo = new 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}", PhotoItem.Name, j);
                                fInfo    = new 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}Playngo_PhotoAlbums/{0}/{1}/"), String.Format("{0}Playngo_PhotoAlbums/{0}/{1}/"), (int)DotNetNuke.Services.FileSystem.FolderController.StorageLocationTypes.InsecureFileSystem);
                            }

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

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



                        statuses.Add(new Resource_FilesStatus(PhotoItem, PortalSettings, ModulePath));
                    }
                }
            }
        }
        public void Execute(BasePage Context)
        {
            //根据ID查询出缩略的方式
            Int32  PhotoID = WebHelper.GetIntParam(Context.Request, "ID", 0);
            Int32  Width   = WebHelper.GetIntParam(Context.Request, "width", 200);
            Int32  height  = WebHelper.GetIntParam(Context.Request, "height", 200);
            String Mode    = WebHelper.GetStringParam(Context.Request, "mode", "AUTO");


            String ImagePath = Context.MapPath(String.Format("{0}/Resource/images/no_image.png", Context.TemplateSourceDirectory));

            Boolean isPush = false;

            if (PhotoID > 0)
            {
                Playngo_ClientZone_Files PhotoItem = Playngo_ClientZone_Files.FindByID(PhotoID);
                if (PhotoItem != null && PhotoItem.ID > 0)
                {
                    //拼接图片的地址
                    //ImagePath = String.Format("{0}ClientZone\\{1}\\{2}\\{3}", PortalSettings.HomeDirectoryMapPath, PhotoItem.ModuleId, PhotoItem.AlbumID, PhotoItem.FileName);
                    ImagePath = Context.MapPath(String.Format("{0}{1}", Context.PortalSettings.HomeDirectory, PhotoItem.FilePath));


                    String ThumbnailPath = String.Format("{0}ClientZone\\thumbnails\\{1}\\{2}_{3}_{4}_{5}.{6}", Context.PortalSettings.HomeDirectoryMapPath, PhotoItem.ModuleId, PhotoItem.ID, Width, height, Mode, PhotoItem.FileExtension);

                    FileInfo ThumbnailFile = new FileInfo(ThumbnailPath);

                    if (!ThumbnailFile.Directory.Exists)
                    {
                        ThumbnailFile.Directory.Create();
                    }


                    isPush = true;

                    if (ThumbnailFile.Exists)
                    {
                        Context.Response.Clear();
                        Context.Response.AddHeader("content-disposition", "attachment;filename=" + Context.Server.UrlEncode(ThumbnailFile.Name));
                        Context.Response.AddHeader("content-length", ThumbnailFile.Length.ToString());
                        Context.Response.ContentType     = PhotoItem.FileMate;
                        Context.Response.Charset         = "utf-8";
                        Context.Response.ContentEncoding = Encoding.UTF8;
                        Context.Response.WriteFile(ThumbnailFile.FullName);
                        Context.Response.Flush();
                        Context.Response.End();
                    }
                    else
                    {
                        GenerateThumbnail.PushThumbnail(ImagePath, ThumbnailPath, Width, height, Mode);
                    }
                }
            }


            if (!isPush)
            {
                FileInfo NoFile = new FileInfo(ImagePath);

                Context.Response.Clear();
                Context.Response.AddHeader("content-disposition", "attachment;filename=" + Context.Server.UrlEncode(NoFile.Name));
                Context.Response.AddHeader("content-length", NoFile.Length.ToString());
                Context.Response.ContentType     = FileSystemUtils.GetContentType(NoFile.Extension.Replace(".", ""));
                Context.Response.Charset         = "utf-8";
                Context.Response.ContentEncoding = Encoding.UTF8;
                Context.Response.WriteFile(NoFile.FullName);
                Context.Response.Flush();
                Context.Response.End();
            }
        }