首页 新闻 会员 周边

silverlight 下载问题,在异步完成事件中实现下载,竟然无法触发handle的方法,直接在事件中可以

0
悬赏园豆:50 [已解决问题] 解决于 2014-07-01 23:30
//HyperlinkButton Click事件 
 private void jy_Click(object sender, RoutedEventArgs e)
        {

            //初始化WCF对象
            WcfServiceClient.WcfServiceClient wcfClient = new WcfServiceClient.WcfServiceClient();
            wcfClient.AddNewSampleInfoCompleted += new EventHandler<AddNewSampleInfoCompletedEventArgs>(ActionAdd);
            wcfClient.AddNewSampleInfoAsync(baseInfo);
        }

在回调方法中实现下载

 void ActionAdd(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            string strPath = @"UploadFiles/20130719/20130719/物流时刻.xls";


            FunctionBase funBase = new FunctionBase();
            funBase.BaseDownFiles(strPath);

        }

下载方法:

 #region 文件下载
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="strPath">文件路径</param>
        public void BaseDownFiles(string strPath)
        {

            //获取系统绝对路径
            string strFullUri = Application.Current.Host.Source.AbsoluteUri;

            //索引,用于截取相对服务器路径
            int index = strFullUri.IndexOf("/ClientBin");

            //服务器路径
            string strUri = strFullUri.Substring(0, index);

            //调用Handle
            string handle = "/DownLoad.ashx?FileName=";

            //对文件路径进行编码
            strPath = UnicodeConvert.ToUnicode(strPath);

            //设置跳转路径
            Uri uri = new Uri(strUri + handle + strPath, UriKind.Absolute);
       
            //操作DOM
            HtmlPage.PopupWindow(uri, "_self", new HtmlPopupWindowOptions());
        }
        #endregion
 private long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
        public void ProcessRequest(HttpContext context)
        {
            //string strLocationPath = HttpContext.Current.Request.Url.ToString().Replace(
            //    HttpContext.Current.Request.Url.ToString().Substring(
            //    HttpContext.Current.Request.Url.ToString().IndexOf("SLContainer"), 12), "");

            string tmpfileName = context.Request.QueryString["FileName"];

            string fileName = UnicodeConvert.FromUnicode(tmpfileName); ;
            string filePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
            if (fileInfo.Exists == true)
            {
                byte[] buffer = new byte[ChunkSize];

                context.Response.Clear();

                FileStream iStream = File.OpenRead(filePath);
                long dataLengthToRead = iStream.Length; //获取下载的文件总大小
                context.Response.AddHeader("Content-Disposition", "attachment;  filename=" +
                   HttpUtility.UrlEncode(fileInfo.Name));
                Utils utils = new Utils();
                context.Response.ContentType = utils.GetContentType(fileInfo.FullName); //"application/octet-stream";
                context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());

                while (dataLengthToRead > 0 && context.Response.IsClientConnected)
                {
                    int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(102400));//'读取的大小

                    context.Response.OutputStream.Write(buffer, 0, lengthRead);
                    context.Response.Flush();
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }
                //context.Response.Close();
                context.Response.End();
宝宝,爸爸爱你的主页 宝宝,爸爸爱你 | 初学一级 | 园豆:57
提问于:2013-07-29 09:24
< >
分享
最佳答案
0
#region 文件下载
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="strPath">文件路径</param>
        public void BaseDownFiles(string strPath)
        {
            //获取系统绝对路径
            string strFullUri = Application.Current.Host.Source.AbsoluteUri;

            //索引,用于截取相对服务器路径
            int index = strFullUri.IndexOf("/ClientBin");

            //服务器路径
            string strUri = strFullUri.Substring(0, index);

            //调用Handle
            string handle = "/DownLoad.ashx?FileName=";

            //对文件路径进行编码
            strPath = UnicodeConvert.ToUnicode(strPath);

            //设置跳转路径
            Uri uri = new Uri(strUri + handle + strPath, UriKind.Absolute);

            //操作DOM
            //HtmlPage.PopupWindow(uri, "_self", new HtmlPopupWindowOptions());
            HtmlPage.Window.Navigate(uri, "_self");
        }
        #endregion
宝宝,爸爸爱你 | 初学一级 |园豆:57 | 2013-07-29 16:19
其他回答(1)
0

我这边的作法就是先下载数据到本地,然后一个一个写入excel中,然后保存下来

收获园豆:50
chenping2008 | 园豆:9836 (大侠五级) | 2013-07-29 09:49

主要是这个下载在click事件中可以实现,但是在异步调用完成的事件中无法实现

支持(0) 反对(0) 宝宝,爸爸爱你 | 园豆:57 (初学一级) | 2013-07-29 15:48

不过谢谢您给出的方案,我自己已经解决了,原因我也不清楚。

主要是文件下载方法中打开文件链接,方法好像使用不正确吧。

#region 文件下载
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="strPath">文件路径</param>
        public void BaseDownFiles(string strPath)
        {
            //获取系统绝对路径
            string strFullUri = Application.Current.Host.Source.AbsoluteUri;

            //索引,用于截取相对服务器路径
            int index = strFullUri.IndexOf("/ClientBin");

            //服务器路径
            string strUri = strFullUri.Substring(0, index);

            //调用Handle
            string handle = "/DownLoad.ashx?FileName=";

            //对文件路径进行编码
            strPath = UnicodeConvert.ToUnicode(strPath);

            //设置跳转路径
            Uri uri = new Uri(strUri + handle + strPath, UriKind.Absolute);

            //操作DOM
            //HtmlPage.PopupWindow(uri, "_self", new HtmlPopupWindowOptions());
            HtmlPage.Window.Navigate(uri, "_self");
        }
        #endregion
支持(0) 反对(0) 宝宝,爸爸爱你 | 园豆:57 (初学一级) | 2013-07-29 16:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册