专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Flex

FileReference and HttpHandler‏上传解决思路

发布时间:2011-06-27 20:45:03 文章来源:www.iduyao.cn 采编人员:星星草
FileReference and HttpHandler‏上传
我在flex里面结合HttpHandler‏实现文件上传,但是不能成功,代码如下,请问哪里有问题,或者谁有例子吗,可以学习下。
.Net HttpHandler‏ 代码如下:(我要把上传的文件存在目录C:\Documents and Settings\robin_lu\Desktop\Web\UploadFiles下,http://localhost:9191/Web/UpLoadHandler.ashx是部署在IIs上面的)
C# code

<%@ WebHandler Language="C#" Class="UpLoadHandler" %>

 

using System;

using System.Web;

 

public class UpLoadHandler : IHttpHandler

{

    public void ProcessRequest(HttpContext context)

    {

        context.Response.ContentType = "text/plain";

        //string tempFile = this.Page.Request.PhysicalApplicationPath;

        string tempFile = @"C:\Documents and Settings\robin_lu\Desktop\Web";

 

        HttpFileCollection files = context.Request.Files;

        if (files.Count > 0)

        {

            //string path = context.Server.MapPath(uploadFolder);

            for (int j = 0; j < files.Count; j++)

            {

 

                HttpPostedFile uploadFile = files[j];

 

                if (uploadFile.ContentLength > 0)

                {

                    uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "UploadFiles\\", uploadFile.FileName));

                }

            }

        }

        else

        {

            context.Response.Write("parameter error");

            context.Response.End();

        }

       

    }

 

    public bool IsReusable

    {

        get

        {

            return false;

        }

    }

 

 

}




flex代码如下:
Java code

<?xml version="1.0" encoding="utf-8"?>
   <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
       <mx:Panel x="49" y="66" width="551" height="164" layout="absolute" 
           title="使用FileReference上传/下载文件" fontSize="12">
           <mx:HDividedBox x="10" y="10" width="511" height="102">
               <mx:Canvas  id="left" backgroundColor="#D7F4FF" height="100%" width="209">
               <mx:TextInput x="4" y="20" id="txtFile" text="{stateText}" width="135"/>
               <mx:Button x="147" y="20" label="选择" fontWeight="normal" click="{file.browse()}"/>
               <mx:Button x="31" y="68" label="上传文件" width="111" fontWeight="normal" click="onUpLoad()"/>
              </mx:Canvas>
              <mx:Canvas id="right" backgroundColor="#D7F4FF" height="100%" width="282">
                  <mx:Label x="6" y="9" text="http://localhost:9191/Web/UploadFiles/AS.txt"/>
                  <mx:Button x="10" y="37" label="下载文件" fontWeight="normal" click="onDownLoad()"/>
                  <mx:Label x="10" y="74" width="272" id="resultLabel"/>
                  <mx:TextInput x="122" y="37" id="downState"/>
              </mx:Canvas>
          </mx:HDividedBox>
          
      </mx:Panel>
          <mx:Script>
          <![CDATA[
          
          
              [Bindable]
              private var stateText:String = "Please select a file";
              
              private var file:FileReference = new FileReference();
              private var fileDown:FileReference = new FileReference();
              
              /**
               * createChildren 比 creationComplete 事件更早发生
               * */
              protected override function createChildren():void
              {
                  super.createChildren();
                  file.addEventListener(Event.SELECT,onSelected);
                  file.addEventListener(Event.COMPLETE,onUploadCompleted);
                  file.addEventListener(ProgressEvent.PROGRESS,onUploadProgress);
                  
                  fileDown.addEventListener(Event.COMPLETE,onDownCompleted);
                  fileDown.addEventListener(ProgressEvent.PROGRESS,onDownProgress);
              }
              
  //            internal function initApp():void
  //            {
  //                file.addEventListener(Event.SELECT,onSelected);
  //                file.addEventListener(Event.COMPLETE,onCompleted);
  //                file.addEventListener(ProgressEvent.PROGRESS,onProgress);
  //            }
              
              internal function onSelected(evt:Event):void
              {
                  stateText = "The selected file:" + file.name;
              }
              
             
              
              
              
              
              internal function onUploadProgress(evt:ProgressEvent):void
              {
                  stateText = "have uploaded: " + Math.round(100 * evt.bytesLoaded / evt.bytesTotal) + "%";
                  
              }
              internal function onUploadCompleted(evt:Event):void
              {
                  stateText = "upload complete!";
              }
              
              
              
              internal function onDownProgress(evt:ProgressEvent):void
              {
                  downState.text = "have downloaded: " + Math.round(100 * evt.bytesLoaded / evt.bytesTotal) + "%";
              }
              internal function onDownCompleted(evt:Event):void
              {
                  var fileRef:FileReference = evt.currentTarget as FileReference;
                  resultLabel.text = "file name:" + fileRef.name + " is downloaded completely!";
              }
              
              /**
               * 调用FileReference的实例方法upload()实现文件上传
               * */
              internal function onUpLoad():void
              {
                  if(file.size > 0)
                  {
                      stateText = "Uploading file:" + file.name;
                  }
                  var request:URLRequest = new URLRequest();
                  
                  request.url="http://localhost:9191/Web/UpLoadHandler.ashx";
                  file.upload(request);
              }
              
              /**
               * 调用FileReference类的实例方法download()实现文件下载
               * */
              internal function onDownLoad():void
              {
                  var request:URLRequest = new URLRequest();
                  request.url="http://localhost:9191/Web/UploadFiles/flex sourcecode.txt";
                  fileDown.download(request);
              }
         ]]>
     </mx:Script>
 </mx:Application>
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: