flex fileupload

展开下面的代码看吧,第一次使用代码模式,有点问题

一、CS相应源码如下:

  1. import flash.net.FileReference;
  2. import flash.net.URLRequest;
  3. import flash.net.FileFilter;
  4. import flash.display.Sprite;
  5. import flash.events.*;
  6. var fileRef = new FileReference();
  7. var fileListener = new Object();
  8. var totalBytes:Number = 0;
  9. var uploadedBytes:Number = 0;
  10. //从外部获取也许上传的文件类型
  11. var acceptFileType:String = getFlashVars("acceptFileType");
  12. //设置上传完毕后执行的js脚本
  13. var comJsFun:String=getFlashVars("ComJsFun");
  14. //this.tbFilePath.text = comJsFun;
  15. //
  16. var uploadURL:URLRequest = new URLRequest();
  17. var xmlRequest:URLRequest = new URLRequest();
  18. var xmlLoader:URLLoader = new URLLoader();
  19. //初始化系统
  20. function init() {
  21.     this.mcWaterStyle.visible = false;
  22.     this.mcFilePlayer.visible = false;
  23.     this.progressBar.visible = false;
  24.     //设置按钮文字大小,默认的字体和大小有点难看。
  25.     var myFormat = new TextFormat();
  26.     myFormat.size = 12;
  27.     this.btBrowser.setStyle("textFormat", myFormat);
  28.     this.btUpload.setStyle("textFormat", myFormat);
  29.     this.tbFilePath.setStyle("textFormat", myFormat);
  30.    
  31.     this.mcWaterStyle.style0.setStyle("textFormat", myFormat);
  32.     this.mcWaterStyle.style1.setStyle("textFormat", myFormat);
  33.     this.mcWaterStyle.style2.setStyle("textFormat", myFormat);
  34.     this.uploadInfo.setStyle("textFormat", myFormat);
  35.    
  36. //如果从FlashVars获取的参数acceptFileType(限定上传文件类型)无值,那么访问相应的XML(上传文件类型配置)
  37.     if(acceptFileType==""){
  38.         //从外部获取上传附加的文件类型。
  39.         xmlRequest = new URLRequest("http://www.cnblogs.com/XML/Setting_Article.xml");
  40.         xmlLoader.load(xmlRequest);
  41.         xmlLoader.addEventListener(Event.COMPLETE,loaderHandler);
  42.     }
  43. }
  44. init();
  45. //按钮事件
  46. this.btBrowser.addEventListener(MouseEvent.CLICK, browseHandler);
  47. this.btUpload.addEventListener(MouseEvent.CLICK, UploadHandler);
  48. fileRef.addEventListener(Event.SELECT,selectHandler);//选择文件
  49. fileRef.addEventListener(Event.CANCEL, cancelHandler);//上传过程中取消事件
  50. fileRef.addEventListener(Event.OPEN, openHandler);//开始上传事件
  51. fileRef.addEventListener(Event.COMPLETE, completeHandler);//上传完毕事件
  52. fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler)//上传数据完毕事件,有点迷惑Event.COMPLETE跟DataEvent.UPLOAD_COMPLETE_DATA的区别
  53. fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);//上传过程事件,相应的进度条代码在这里了
  54. //监听上传过程可能出错的事件
  55. fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
  56. fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  57. fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  58. //浏览按钮事件
  59. function browseHandler(event:MouseEvent):void {
  60.     fileRef.browse(browseGetTypes());
  61. }
  62. function browseGetTypes():Array {
  63.     var allTypes:Array = new Array(browseGetFileTypeFilter());
  64.     return allTypes;
  65. }
  66. function browseGetFileTypeFilter():FileFilter {
  67.    
  68.     if(acceptFileType!=""){
  69.         var arr = acceptFileType.split("|");
  70.         var FileExtension = "";
  71.         for(var i=0;i<arr.length;i++){
  72.             if (arr == "")
  73.                 continue;
  74.             if(i==0)
  75.                 FileExtension = "*."+arr;
  76.             else
  77.                 FileExtension = FileExtension+";*."+arr;
  78.         }
  79.         return new FileFilter("文件格式("+FileExtension+")", ""+FileExtension+"");
  80.     }else{
  81.         return new FileFilter("所有文件(*.*)","*.*");
  82.     }
  83.    
  84.     //return new FileFilter("所有文件(*.*)","*.*");
  85. }
  86. //加载上传配置事件
  87. function loaderHandler(event:Event):void {
  88.     var myXML:XML = new XML(xmlLoader.data);
  89.     //myXML = XML();
  90.     acceptFileType = myXML.child("Upload").ContontFileStyle;
  91.     trace("--"+myXML.child("Upload").ContontFileStyle+"--");
  92.     if(this.uploadInfo.text=="")
  93.         this.uploadInfo.text = "允许的格式:"+myXML.child("Upload").ContontFileStyle;
  94. }
  95. //选择文件事件
  96. function selectHandler(event:Event):void{
  97.     this.uploadInfo.text = "";
  98.     this.mcWaterStyle.visible = false;
  99.     this.mcFilePlayer.visible = false;
  100.    
  101.     if (fileRef.size > 0){
  102.         totalBytes = fileRef.size;
  103.         this.tbFilePath.text =  fileRef.name+ "[" + this.getSizeType(totalBytes) + "]";
  104.         var fileExtName = fileRef.name.substring(fileRef.name.lastIndexOf(".")+1);
  105.         switch(fileExtName.toLowerCase()){
  106.             case "jpg":
  107.             case "jpeg":
  108.             case "gif":
  109.             case "bmp":
  110.                 this.mcWaterStyle.visible = true;
  111.                 break;
  112.             case "flv":
  113.                 this.mcFilePlayer.visible = true;
  114.                 this.mcFilePlayer.mcWidth.text = 410;
  115.                 this.mcFilePlayer.mcHeight.text = 370;
  116.                 break;
  117.             case "swf":
  118.                 this.mcFilePlayer.visible = true;
  119.                 this.mcFilePlayer.mcWidth.text = 550;
  120.                 this.mcFilePlayer.mcHeight.text = 400;
  121.                 break;
  122.             case "rm":
  123.             case "rmvb":
  124.             case "mp3":
  125.             case "avi":
  126.             case "mpg":
  127.             case "mpeg":
  128.             case "asf":
  129.             case "wmv":
  130.             case "wma":
  131.                 this.mcFilePlayer.visible = true;
  132.                 this.mcFilePlayer.mcWidth.text = 550;
  133.                 this.mcFilePlayer.mcHeight.text = 400;
  134.                 break;
  135.             default:
  136.                 this.mcWaterStyle.visible = false;
  137.                 this.mcFilePlayer.visible = false;
  138.                 break;
  139.         }
  140.     }else{
  141.         this.uploadInfo.text = "错误:您没有选择文件!";
  142.     }
  143. }
  144. //上传过程中取消
  145. function cancelHandler(event:Event):void{
  146.     this.progressBar.visible = false;
  147. };
  148. //当上载或下载操作开始时
  149. function openHandler(event:Event):void{
  150.     this.tbFilePath.visible = false;
  151.     this.mcWaterStyle.visible = false;
  152.     this.mcFilePlayer.visible = false;
  153.     this.btBrowser.visible = false;
  154.     this.btUpload.label = "取消";
  155.     this.progressBar.visible = true;
  156. };
  157. //点击伤
  158. function UploadHandler(event:MouseEvent):void{
  159.     if (this.btUpload.label=="上传"){
  160.         var WaterMarkStyleP:String = "";
  161.         if(this.mcWaterStyle.style0.selected == true)
  162.             WaterMarkStyleP = "0";
  163.         else if(this.mcWaterStyle.style1.selected == true)
  164.             WaterMarkStyleP = "1";
  165.         else if(this.mcWaterStyle.style2.selected == true)
  166.             WaterMarkStyleP = "2";
  167.        
  168.         var mcFilePlayerP:String="&width="+this.mcFilePlayer.mcWidth;
  169.         mcFilePlayerP += "&height="+this.mcFilePlayer.mcHeigth;
  170.        
  171.         if(this.mcFilePlayer.mcAutoYes.selected == true)
  172.             mcFilePlayerP += "&auto=true";
  173.         else if(this.mcFilePlayer.mcAutoNo.selected == true)
  174.             mcFilePlayerP += "&auto=false";
  175.            
  176.         uploadURL.url="Upload_File_SWF.aspx?WaterMarkStyle="+WaterMarkStyleP+mcFilePlayerP;
  177.         fileRef.upload(uploadURL);//此为采用aspx默认方式进行上传的文件地址。如:Upload_File_SWF.aspx此页面我在上传完毕后,直接进行Response.Write("true");那么Flash可在uploadCompleteDataHandler后可后去到true这个数据。以便你进行操作。
  178.     }else if(this.btUpload.label=="取消"){
  179.         this.tbFilePath.visible = true;
  180.         this.mcWaterStyle.visible = false;
  181.         this.mcFilePlayer.visible = false;
  182.         this.btBrowser.visible = true;
  183.         this.btUpload.label = "上传";
  184.         this.progressBar.visible = false;
  185.         this.uploadInfo.text="";
  186.     }else if(this.btUpload.label=="重新上传"){
  187.         this.tbFilePath.visible = true;
  188.         this.mcWaterStyle.visible = false;
  189.         this.mcFilePlayer.visible = false;
  190.         this.btBrowser.visible = true;
  191.         this.btUpload.label = "上传";
  192.         this.progressBar.visible = false;
  193.     }
  194. }
  195. //在文件上载或下载操作期间定期调用
  196. function progressHandler(event:ProgressEvent):void{
  197.     //var fileRef:FileReference = FileReference(event.target);
  198.     if(event.bytesLoaded==event.bytesTotal){
  199.         this.uploadInfo.text= "正在转移数据,请稍后--"+getSizeType(event.bytesLoaded)+"/"+getSizeType(event.bytesTotal);
  200.     }else{
  201.         this.progressBar.mcMask.width = (event.bytesLoaded/event.bytesTotal)*this.progressBar.mcLoaded.width;
  202.         this.uploadInfo.text="上传文件中,请等待--"+getSizeType(event.bytesLoaded)+"/"+getSizeType(event.bytesTotal);
  203.     }
  204. }
  205. function completeHandler(event:Event):void {
  206.     //trace("completeHandler: " + event);
  207.     this.tbFilePath.visible = true;
  208.     this.tbFilePath.text = "";
  209.     this.mcWaterStyle.visible = false;
  210.     this.mcFilePlayer.visible = false;
  211.     this.btBrowser.visible = true;
  212.     this.btUpload.label = "上传";
  213.     this.progressBar.visible = false;
  214.     //this.uploadInfo.text = "上传成功!";
  215. }
  216. function uploadCompleteDataHandler(event:DataEvent):void {
  217.     if(event.data.indexOf("|")!=-1){
  218.         var fileInfoArr = event.data.split("|");
  219.         if(fileInfoArr[0].toLowerCase()=="true"){
  220.             this.uploadInfo.text = "上传成功:"+fileInfoArr[1]+"!";
  221.             if(comJsFun!=""){
  222.                 //ExternalInterface.call("UploadForEditor","hh");
  223.                 ExternalInterface.call("UploadForEditor",event.data.replace("true|",""));
  224.                 //this.tbFilePath.text = event.data;
  225.             }
  226.         }else{
  227.             this.uploadInfo.text = "上传失败:"+EncodeUtf8(fileInfoArr[1])+"!";
  228.         }
  229.     }
  230.    
  231.    
  232.    
  233.     trace("uploadCompleteData: " + event.data);
  234. }
  235. //错误事件
  236. function httpStatusHandler(event:HTTPStatusEvent):void {
  237.   trace("httpStatusHandler: " + event);
  238.   this.mcWaterStyle.visible = false;
  239.   this.mcFilePlayer.visible = false;
  240.   this.uploadInfo.text="HTTP错误: " +  event;
  241. }
  242. function ioErrorHandler(event:IOErrorEvent):void {
  243.   trace("ioErrorHandler: " + event);
  244.   this.mcWaterStyle.visible = false;
  245.   this.mcFilePlayer.visible = false;
  246.   this.uploadInfo.text="IO错误: " +  event;
  247. }
  248. function securityErrorHandler(event:SecurityErrorEvent):void {
  249.   trace("openHandler: " + event);
  250.   this.mcWaterStyle.visible = false;
  251.   this.mcFilePlayer.visible = false;
  252.   this.uploadInfo.text="IO安全设置错误: " +  event;
  253. }
  254. /**//*--------------------以下为常用函数-------------
  255. ---------------------------------------------*/
  256. //处理文件大小表示方法
  257. function getSizeType(s)
  258. {
  259.     var danwei = ["Byte","KB","MB","GB" ];
  260.     var d = 0;
  261.     while ( s >= 900 )
  262.     {
  263.         s = Math.round(s*100/1024)/100;
  264.         d++;
  265.     }
  266.     return s+danwei[d];
  267. }
  268. function getFlashVars(parName){
  269.     var parValue:String=stage.loaderInfo.parameters[parName];
  270.     if(parValue==null)
  271.       return "";
  272.     else
  273.       return parValue;
  274. }
  275. //转换乱码
  276. function EncodeUtf8(str : String):String {
  277.     var oriByteArr : ByteArray = new ByteArray();
  278.     oriByteArr.writeUTFBytes(str);
  279.     var tempByteArr : ByteArray = new ByteArray();
  280.     for (var i = 0; i<oriByteArr.length; i++) {
  281.         if (oriByteArr == 194) {
  282.             tempByteArr.writeByte(oriByteArr[i+1]);
  283.             i++;
  284.         } else if (oriByteArr == 195) {
  285.             tempByteArr.writeByte(oriByteArr[i+1] + 64);
  286.             i++;
  287.         } else {
  288.             tempByteArr.writeByte(oriByteArr);
  289.         }
  290.     }
  291.     tempByteArr.position = 0;
  292.     return tempByteArr.readMultiByte(tempByteArr.bytesAvailable,"chinese");
  293.     //return tempByteArr.readMultiByte(tempByteArr.bytesAvailable,"chinese");
  294. }
复制代码

二、相应的上传文件代码(.net)

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. using System.Drawing;
  13. using System.Drawing.Imaging;
  14. namespace TT.WebManager.Web.Admin.Article
  15. {
  16.     public partial class Upload_File_SWF : System.Web.UI.Page
  17.     {
  18.         string AppPath = "";
  19.         string FileURL = "";//文件网络地址
  20.         string FileURL2 = "";//文件网络地址,如果加了水印
  21.         string FileAppPath = "";
  22.         string FileAppPath2 = "";
  23.         string sFilePathName = "";
  24.         string sFileName = "";
  25.         int FileSize = 0;
  26.         string FileExtName = "";
  27.         int WaterMarkStyle = 0;
  28.         string FileAuto = "true";
  29.         int FileWidth = 0;
  30.         int FileHeigth = 0;
  31.         protected void Page_Load(object sender, EventArgs e)
  32.         {
  33.             TT.WebManager.BLL.Setting bll = new TT.WebManager.BLL.Setting();
  34.             bll.Init(false);
  35.             AppPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
  36.             WaterMarkStyle = Methods.Req.GetInt("WaterMarkStyle", 0);
  37.             FileWidth = Methods.Req.GetInt("Width",550);
  38.             FileHeigth = Methods.Req.GetInt("Heigth", 400);
  39.             FileAuto = Methods.Req.GetString("Auto");
  40.             try
  41.             {
  42.                 HttpPostedFile postedFile = Request.Files["Filedata"];
  43.                 sFilePathName = postedFile.FileName;
  44.                 FileSize = postedFile.ContentLength;  //===========获取文件的大小
  45.                 if (FileSize <= 0)
  46.                 {
  47.                     Response.Clear();
  48.                     Response.Write("false|您没有选择文件!");
  49.                     Response.End();
  50.                 }
  51.                 else
  52.                 {
  53.                     sFileName = sFilePathName.Substring(sFilePathName.LastIndexOf("\\") + 1);
  54.                     FileExtName = sFilePathName.Substring(sFilePathName.LastIndexOf(".") + 1);
  55.                     if (IsValidFileType(FileExtName) == false)
  56.                     {
  57.                         Response.Clear();
  58.                         Response.Write("false|系统禁止上传此格式的文件!");
  59.                         Response.End();
  60.                     }
  61.                     else
  62.                     {
  63.                         GotoUploadFile(postedFile);
  64.                     }
  65.                 }
  66.             }
  67.             catch { }
  68.            
  69.         }
  70.         protected void GotoUploadFile(HttpPostedFile postedFile)
  71.         {
  72.             string NewFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + FileSize + "." + FileExtName;
  73.             string NewFileName2 = DateTime.Now.ToString("yyyyMMddhhmmss") + FileSize + "_1." + FileExtName;
  74.             string NewFilePath = "";
  75.             if (Model.Article.Setting.Upload_SortFile == true)
  76.             {
  77.                 if (Model.Article.Setting.Upload_FolderStyle != "")
  78.                 {
  79.                     FileURL = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + FileExtName + "/" + DateTime.Now.ToString(Model.Article.Setting.Upload_FolderStyle) + "/" + NewFileName;
  80.                     FileURL2 = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + FileExtName + "/" + DateTime.Now.ToString(Model.Article.Setting.Upload_FolderStyle) + "/" + NewFileName2;
  81.                     NewFilePath = AppPath + Model.Article.Setting.Upload_Folder.Replace("/", "\\") + "\\" + FileExtName + "\\" + DateTime.Now.ToString(Model.Article.Setting.Upload_FolderStyle).Replace("/", "\\");
  82.                 }
  83.                 else
  84.                 {
  85.                     FileURL = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + FileExtName + "/" + NewFileName;
  86.                     FileURL2 = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + FileExtName + "/" + NewFileName2;
  87.                     NewFilePath = AppPath + Model.Article.Setting.Upload_Folder.Replace("/", "\\") + "\\" + FileExtName;
  88.                 }
  89.             }
  90.             else
  91.             {
  92.                 if (Model.Article.Setting.Upload_FolderStyle != "")
  93.                 {
  94.                     FileURL = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + DateTime.Now.ToString(Model.Article.Setting.Upload_FolderStyle) + "/" + NewFileName;
  95.                     FileURL2 = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + DateTime.Now.ToString(Model.Article.Setting.Upload_FolderStyle) + "/" + NewFileName2;
  96.                     NewFilePath = AppPath + Model.Article.Setting.Upload_Folder.Replace("/", "\\") + "\\" + DateTime.Now.ToString(Model.Article.Setting.Upload_FolderStyle).Replace("/", "\\");
  97.                 }
  98.                 else
  99.                 {
  100.                     FileURL = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + NewFileName;
  101.                     FileURL2 = Model.Article.Setting.Upload_Prefix + Model.Article.Setting.Upload_Folder + "/" + NewFileName2;
  102.                     NewFilePath = AppPath + Model.Article.Setting.Upload_Folder.Replace("/", "\\");
  103.                 }
  104.             }
  105.             TT.WebManager.Methods.IIOO.CreateDirectory(NewFilePath);
  106.             FileAppPath = NewFilePath + "\\" + NewFileName;
  107.             FileAppPath2 = NewFilePath + "\\" + NewFileName2;
  108.             postedFile.SaveAs(FileAppPath);
  109.             switch (FileExtName.ToLower())
  110.             {
  111.                 case "jpg":
  112.                 case "jpeg":
  113.                 case "gif":
  114.                 case "bmp":
  115.                 case "png":
  116.                     //==========生成水印=============
  117.                     switch (WaterMarkStyle)
  118.                     {
  119.                         case 1:
  120.                             CreateWeaterText(FileAppPath, FileAppPath2, Model.Article.Setting.WaterMark_Text, FileExtName);
  121.                             FileURL = FileURL2;
  122.                             break;
  123.                         case 2:
  124.                             CreateWeaterPicture(FileAppPath, FileAppPath2, Server.MapPath(Model.Article.Setting.WaterMark_Picture), FileExtName);
  125.                             FileURL = FileURL2;
  126.                             break;
  127.                         default:
  128.                             break;
  129.                     }
  130.                     break;
  131.             }
  132.             string ReturnString = sFileName + "|" + FileURL + "|" + FileWidth + "|" + FileHeigth + "|" + FileAuto;
  133.             Response.Clear();
  134.             Response.Write("true|"+ReturnString);
  135.             Response.End();
  136.         }
  137.         private void CreateWeaterText(string paraOldFileName, string paraNewsFileName, string AddText, string FileExtensionName)
  138.         {
  139.             System.Drawing.Image image = System.Drawing.Image.FromFile(paraOldFileName);
  140.             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
  141.             //文字抗锯齿
  142.             g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  143.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  144.             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  145.             g.DrawImage(image, 0, 0, image.Width, image.Height);
  146.             //Font font = new Font("黑体", 14, FontStyle.Bold);
  147.             //Brush b = new SolidBrush(Color.Yellow);
  148.             Font font = new Font(Model.Article.Setting.WaterMark_FontFamily, Model.Article.Setting.WaterMark_FontSize, FontStyle.Bold);
  149.             Brush b = new SolidBrush(Methods.TypeParse.StrToColor(Model.Article.Setting.WaterMark_FontColor));
  150.             int Xpos = 0;
  151.             int Ypos = 0;
  152.             SizeF crsize = new SizeF();
  153.             crsize = g.MeasureString(AddText, font);
  154.             int crsize_With = Convert.ToInt32(crsize.Width);
  155.             int crsize_Height = Convert.ToInt32(crsize.Height);
  156.             int WaterMark_xPos = 10;
  157.             int WaterMark_yPos = 10;
  158.             switch (Model.Article.Setting.WaterMark_Position)//==========从图片的左上角开始算起(0,0)
  159.             {
  160.                 case 1:
  161.                     Xpos = WaterMark_xPos;
  162.                     Ypos = WaterMark_yPos;
  163.                     break;
  164.                 case 2:
  165.                     Xpos = image.Width - (crsize_With + WaterMark_xPos);
  166.                     Ypos = WaterMark_yPos;
  167.                     break;
  168.                 case 3:
  169.                     Xpos = WaterMark_xPos;
  170.                     Ypos = image.Height - (crsize_Height + WaterMark_yPos);
  171.                     break;
  172.                 case 4:
  173.                     Xpos = image.Width - (crsize_With + WaterMark_xPos);
  174.                     Ypos = image.Height - (crsize_Height + WaterMark_yPos);
  175.                     break;
  176.             }
  177.             //画阴影
  178.             PointF pt = new PointF(0, 0);
  179.             System.Drawing.Brush TransparentBrush0 = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(50, System.Drawing.Color.Black));
  180.             System.Drawing.Brush TransparentBrush1 = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(20, System.Drawing.Color.Black));
  181.             g.DrawString(AddText, font, TransparentBrush0, Xpos, Ypos + 1);
  182.             g.DrawString(AddText, font, TransparentBrush0, Xpos + 1, Ypos);
  183.             g.DrawString(AddText, font, TransparentBrush1, Xpos + 1, Ypos + 1);
  184.             g.DrawString(AddText, font, TransparentBrush1, Xpos, Ypos + 2);
  185.             g.DrawString(AddText, font, TransparentBrush1, Xpos + 2, Ypos);
  186.             TransparentBrush0.Dispose();
  187.             TransparentBrush1.Dispose();
  188.             g.DrawString(AddText, font, b, Xpos, Ypos);
  189.             switch (FileExtensionName.ToLower())
  190.             {
  191.                 case "jpg":
  192.                 case "jpeg":
  193.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  194.                     break;
  195.                 case "gif":
  196.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
  197.                     break;
  198.                 case "bmp":
  199.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Bmp);
  200.                     break;
  201.                 case "png":
  202.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
  203.                     break;
  204.             }
  205.             g.Dispose();
  206.             image.Save(paraNewsFileName);
  207.             image.Dispose();
  208.             Response.Clear();
  209.             if (File.Exists(paraOldFileName) == true)
  210.             {
  211.                 File.Delete(paraOldFileName);
  212.             }
  213.         }
  214.         private void CreateWeaterPicture(string paraOldFileName, string paraNewsFileName, string AddPicture, string FileExtensionName)
  215.         {
  216.             System.Drawing.Image image = System.Drawing.Image.FromFile(paraOldFileName);
  217.             System.Drawing.Image copyImage = System.Drawing.Image.FromFile(AddPicture);
  218.             ImageAttributes imageAttributes = new ImageAttributes();
  219.             ColorMap colorMap = new ColorMap();
  220.             colorMap.OldColor = Color.FromArgb(200, 0, 200, 0);
  221.             colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
  222.             ColorMap[] remapTable = { colorMap };
  223.             imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
  224.             float[][] colorMatrixElements = {
  225.                                                 new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
  226.                                                 new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
  227.                                                 new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
  228.                                                 new float[] {0.0f,  0.0f,  0.0f,  Model.Article.Setting.WaterMark_Transparency, 0.0f},
  229.                                                 new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
  230.                                             };
  231.             ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  232.             imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  233.             Graphics g = Graphics.FromImage(image);
  234.             //PointF pt = new PointF(0, 0);
  235.             //System.Drawing.Brush TransparentBrush0 = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(50, System.Drawing.Color.));
  236.             //System.Drawing.Brush TransparentBrush1 = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(20, System.Drawing.Color.Black));
  237.             //g.DrawString(AddText, font, TransparentBrush0, pt.X, pt.Y + 1);
  238.             //g.DrawString(AddText, font, TransparentBrush0, pt.X + 1, pt.Y);
  239.             //g.DrawString(AddText, font, TransparentBrush1, pt.X + 1, pt.Y + 1);
  240.             //g.DrawString(AddText, font, TransparentBrush1, pt.X, pt.Y + 2);
  241.             //g.DrawString(AddText, font, TransparentBrush1, pt.X + 2, pt.Y);
  242.             //TransparentBrush0.Dispose();
  243.             //TransparentBrush1.Dispose();
  244.             //设定合成图像的质量
  245.             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  246.             //设置高质量插值法
  247.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  248.             //设置高质量,低速度呈现平滑程度
  249.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  250.             int xPos = image.Width - copyImage.Width - 10;
  251.             int yPos = image.Height - copyImage.Height - 10;
  252.             switch (Model.Article.Setting.WaterMark_Position)//==========从图片的左上角开始算起(0,0)
  253.             {
  254.                 case 1:
  255.                     xPos = 10;
  256.                     yPos = 10;
  257.                     break;
  258.                 case 2:
  259.                     xPos = image.Width - copyImage.Width - 10;
  260.                     yPos = 10;
  261.                     break;
  262.                 case 3:
  263.                     xPos = 10;
  264.                     yPos = image.Height - copyImage.Height - 10;
  265.                     break;
  266.                 case 4:
  267.                     xPos = image.Width - copyImage.Width - 10;
  268.                     yPos = image.Height - copyImage.Height - 10;
  269.                     break;
  270.             }
  271.             g.DrawImage(copyImage, new Rectangle(xPos, yPos, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttributes);
  272.             switch (FileExtensionName.ToLower())
  273.             {
  274.                 case "jpg":
  275.                 case "jpeg":
  276.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  277.                     break;
  278.                 case "gif":
  279.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
  280.                     break;
  281.                 case "bmp":
  282.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Bmp);
  283.                     break;
  284.                 case "png":
  285.                     image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
  286.                     break;
  287.             }
  288.             g.Dispose();
  289.             image.Save(paraNewsFileName);
  290.             image.Dispose();
  291.             imageAttributes.Dispose();
  292.             Response.Clear();
  293.             if (File.Exists(paraOldFileName) == true)
  294.             {
  295.                 File.Delete(paraOldFileName);
  296.             }
  297.             // return paraNewsFileName;
  298.         }
  299.         protected bool IsValidFileType(string ExtName)
  300.         {
  301.             if (Model.Article.Setting.Upload_ContontFileStyle.Trim() == "")
  302.                 return true;
  303.             string[] AcceptedFileTypes = Model.Article.Setting.Upload_ContontFileStyle.Split(new char[] { '|' });
  304.             for (int i = 0; i < AcceptedFileTypes.Length; i++)
  305.             {
  306.                 if (ExtName.ToLower() == AcceptedFileTypes.ToLower())
  307.                 {
  308.                     return true;
  309.                 }
  310.             }
  311.             return false;
  312.         }
  313.     }
  314. }
复制代码

三、相应的aspx页面代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4.   <title>FLASH 上传测试</title>
  5.   <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  6.   <link href="../images/style.css" rel="stylesheet" type="text/css" />
  7.   <script language="javascript">AC_FL_RunContent = 0;</script>
  8.   <script src="../javascript/AC_RunActiveContent.js" language="javascript"></script>
  9.  
  10. </head>
  11. <body>
  12. <!--影片中使用的 URL-->
  13. <!--影片中使用的文本-->
  14. <!-- saved from url=(0013)about:internet -->
  15. <script language="javascript">
  16.     if (AC_FL_RunContent == 0) {
  17.         alert("此页需要 AC_RunActiveContent.js");
  18.     } else {
  19.         AC_FL_RunContent(
  20.             'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
  21.             'width', '400',
  22.             'height', '56',
  23.             'src', '../SWFUpload/SWFFileUpload',
  24.             'quality', 'high',
  25.             'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
  26.             'align', 'middle',
  27.             'play', 'true',
  28.             'loop', 'true',
  29.             'scale', 'showall',
  30.             'wmode', 'window',
  31.             'devicefont', 'false',
  32.             'id', 'SWFFileUpload',
  33.             'bgcolor', '#ffffff',
  34.             'name', 'SWFFileUpload',
  35.             'menu', 'true',
  36.             'allowFullScreen', 'false',
  37.             'allowScriptAccess','sameDomain',
  38.             'movie', '../SWFUpload/SWFFileUpload',
  39.             'salign', '',
  40.             'FlashVars','ComJsFun=UploadForEditor'
  41.             ); //end AC code
  42.     }
  43. //src和movie属性为Flash文件地址,请勿写.swf后缀名
  44. //FlashVars为给Flash设置的参数。
  45. </script>
  46. <noscript>
  47.     <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="400" height="56" id="SWFFileUpload" align="middle">
  48.     <param name="allowScriptAccess" value="sameDomain" />
  49.     <param name="allowFullScreen" value="false" />
  50.     <param name="movie" value="../SWFUpload/SWFFileUpload.swf" />
  51.     <param name="quality" value="high" />
  52.     <param name="bgcolor" value="#ffffff" />
  53.     <param name="FlashVars" value="ComJsFun=UploadForEditor"/>
  54.     <embed src="../SWFUpload/SWFFileUpload.swf" quality="high" bgcolor="#ffffff" width="400" height="56" name="SWFFileUpload" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
  55.     </object>
  56. </noscript>
  57. <div id="showInfo"></div>
  58. <script language="javascript" type="text/javascript">
  59. function UploadForEditor(Value){
  60.   alert(Value);
  61.   //var RSArr = Value.split("|");
  62.   //UploaFile(RSArr[0],RSArr[1],RSArr[2],RSArr[3],RSArr[4]);
  63. }
  64. </script>
  65. </body>
  66. </html>
复制代码

源码下载:附件: SWFFileUpload.rar (下载 665 次)

原文地址:https://www.cnblogs.com/amylis_chen/p/2742810.html