//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();
#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
我这边的作法就是先下载数据到本地,然后一个一个写入excel中,然后保存下来
主要是这个下载在click事件中可以实现,但是在异步调用完成的事件中无法实现
不过谢谢您给出的方案,我自己已经解决了,原因我也不清楚。
主要是文件下载方法中打开文件链接,方法好像使用不正确吧。
#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