Flex加载文件数据流


  Flex加载文件数据流
  本文使用URLStream,URLRequest,Loader实现数据流的加载,文件可以是图片、SWF等。
  一、Flex代码    加载的图片大小暂未找到获取方法,只好在预知图片大小的情况下将loader适当的缩放一定级别 loader.scaleX = 0.6; loader.scaleY = 0.6 //Loader 对象只能有一个子显示对象,只能选择加载到Image或SWFLoader //image.addChild(loader); mySwfLoader.addChild(loader); } private function onLoaderHandler(event:Event):void { //Loader.loadBytes()时Event.COMPLETE不起作用??!! Alert.show(loader.width.toString()); } private function setSource(event:MouseEvent):void { var str:String = "http://localhost:2286/GetFile.aspx?path=DYDZ20090 7&file=4.swf"; mySwfLoader.source = str; } ]]-->   加载图片" click="loadPicture(event)"/> 加载到swfLoader" click="setSource(event)"/>      二、HttpService生成文件数据流代码  protected void Page_Load(object sender, EventArgs e) { string file = Request["file"]; if (string.IsNullOrEmpty(file)) return; string filepath = Server.MapPath("~/Download/" + file); if (File.Exists(filepath)) DownLoadStreamFile(filepath); Response.End(); } ///  /// 下载文件 ///  ///  ///  public static void ToDownload(string serverfilpath, string filename) { FileStream fileStream = new FileStream(serverfilpath, FileMode.Open); long fileSize = fileStream.Length; HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Di sposition", "attachment; filename=\"" + UTF_FileName(filename) + "\";"); ////attachment --- 作为附件下载 ////inline --- 在线打开 HttpContext.Current.Response.AddHeader("Content-Le ngth", fileSize.ToString()); byte[] fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); HttpContext.Current.Response.BinaryWrite(fileBuffe r); fileStream.Close(); HttpContext.Current.Response.End(); } ///  /// 打开文件 ///  ///  ///  public static void ToOpen(string serverfilpath, string filename) { FileStream fileStream = new FileStream(serverfilpath, FileMode.Open); long fileSize = fileStream.Length; HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Di sposition", "inline; filename=\"" + UTF_FileName(filename) + "\";"); HttpContext.Current.Response.AddHeader("Content-Le ngth", fileSize.ToString()); byte[] fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); HttpContext.Current.Response.BinaryWrite(fileBuffe r); fileStream.Close(); HttpContext.Current.Response.End(); } private static string UTF_FileName(string filename) { return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8); } ///  /// 下载文件数据流 ///  ///  public static void DownLoadStreamFile(string serverfilpath) { int blocksize = 1024 * 100; byte[] buffer = new byte[blocksize]; byte[] fileHeader = new byte[101]; int startindex = 0; int bytesread = 0; int bytesfinish = 0; int filesize = 0; string headerText = string.Empty; BinaryReader bReader = null; bReader = new BinaryReader(File.OpenRead(serverfilpath)); filesize = Convert.ToInt32(bReader.BaseStream.Length); while (bytesfinish 加载时获取不到图片等文件的大小,显示时不精确。

你可能感兴趣的:(Flex)