/// <summary> /// 推送缩略图到页面流 /// </summary> /// <param name="pathImageFrom"></param> /// <param name="width"></param> /// <param name="height"></param> public static void PushThumbnail(string pathImageFrom, string ThumbnailPath, int width, int height, string mode) { MemoryStream s = MakeThumbnail(pathImageFrom, width, height, mode); try { if (s != null && s.Length > 0) { //byte[] bytes = new byte[s.Length]; //s.Write(bytes, 0, bytes.Length); Image image = Image.FromStream(s); image.Save(ThumbnailPath); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentType = FileSystemUtils.GetContentType(Path.GetExtension(pathImageFrom).Replace(".", "")); HttpContext.Current.Response.BinaryWrite(s.ToArray()); } }catch { } finally { if (s != null && s.Length > 0) { s.Close(); s.Dispose(); } HttpContext.Current.Response.End(); } }
public Int32 SynchronizeAllFiles(FileInfo SynchronizeFile) { Int32 SynchronizeFileCount = 0; Playngo_ClientZone_Files PhotoItem = new Playngo_ClientZone_Files(); PhotoItem.ModuleId = ModuleId; PhotoItem.PortalId = PortalId; PhotoItem.FileName = SynchronizeFile.Name; PhotoItem.FileSize = Convert.ToInt32(SynchronizeFile.Length / 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(PhotoItem.FileName).Replace(Path.GetExtension(PhotoItem.FileName), ""); PhotoItem.Status = (Int32)EnumFileStatus.Approved; try { if (("png,gif,jpg,jpeg,bmp").IndexOf(PhotoItem.FileExtension) >= 0) { //图片的流 System.Drawing.Image image = System.Drawing.Image.FromFile(SynchronizeFile.FullName); 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 = PhotoItem.Name; //文件名称 String WebPath = String.Format("ClientZone/{0}/{1}/{2}/{3}/", SaveDirPath, PhotoItem.LastTime.Year, PhotoItem.LastTime.Month, PhotoItem.LastTime.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(); } //异步移动文件到文件夹中 List <String> SynchronizeFileQueue = new List <string>(); SynchronizeFileQueue.Add(SynchronizeFile.FullName); SynchronizeFileQueue.Add(fInfo.FullName); ManagedThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMoveTo), SynchronizeFileQueue); } 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(); if (PhotoId > 0) { SynchronizeFileCount++; } return(SynchronizeFileCount); }
// 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(); } }