sivelrlight 结合asp.net用于大文件下载
来源:互联网 发布:林清轩芦荟胶 知乎 编辑:程序博客网 时间:2024/04/28 14:15
一、在web项目下添加一个“一般处理程序”
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;namespace Silverlightdownload.Web{ /// <summary> /// FileDownLoader 的摘要说明 /// </summary> public class FileDownLoader : IHttpHandler { private long ChunkSize = 20;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 public void ProcessRequest(HttpContext context) { // String fileName = context.Request.QueryString["fileName"]; //客户端传来的文件名 //String filePath = context.Server.MapPath("Files/" + fileName); //要下载文件的路径 String filePath = context.Request.QueryString["filePath"]; String fileName = filePath.Substring(filePath.LastIndexOf('/') + 1); System.IO.Stream iStream = null; byte[] buffer = new byte[ChunkSize]; // Buffer to read 10K bytes in chunk: int lengthRead;//读取的大小 long dataLengthToRead;//获得下载文件的总大小 context.Response.Clear(); System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true) { try { iStream = System.IO.File.OpenRead(filePath); dataLengthToRead = iStream.Length; context.Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); UserLog.WriteErrLog("dataLengthToRead", dataLengthToRead.ToString()); while (dataLengthToRead > 0 && context.Response.IsClientConnected) { if (context.Response.IsClientConnected) { lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize)); context.Response.OutputStream.Write(buffer, 0, lengthRead); context.Response.Flush(); dataLengthToRead = dataLengthToRead - lengthRead; UserLog.WriteErrLog("dataLengthToRead", dataLengthToRead.ToString()); } else { dataLengthToRead = -1; } } } catch (Exception ex) { UserLog.WriteErrLog("LocalDownloadError", ex.ToString()); return; } finally { if (iStream != null) { //Close the file. iStream.Close(); } } context.Response.Clear();//注意是clear(),不是close(),close()对空文件报错 context.Response.End(); } } public bool IsReusable { get { return false; } } }}
二、在页面中添加一个HyperlinkButton控件
<UserControl x:Class="Silverlightdownload.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <HyperlinkButton Name="hyLinkDownLoad" Content="下载文件" HorizontalAlignment="Left" Height="17" Margin="67,83,0,0" VerticalAlignment="Top" Width="73" Click="hyLinkDownLoad_Click" Cursor="Hand"/> </Grid></UserControl>
三、添加点击事件
private void hyLinkDownLoad_Click(object sender, RoutedEventArgs e) { string fileName = "C:\\Users\\dell\\Desktop\\ceshi\\hudd1.txt";//要下载的文件名 Uri uri = new Uri("/FileDownLoader.ashx?filePath=" + fileName, UriKind.Relative); this.hyLinkDownLoad.NavigateUri = uri; }
1 0
- sivelrlight 结合asp.net用于大文件下载
- Asp.net大文件下载
- Asp.net 实现断点续传 下载大文件
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中支持断点续传下载大文件
- 在ASP.NET中支持断点续传下载大文件
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中跟踪和恢复大文件下载
- 在ASP.NET中支持断点续传下载大文件
- ASP.NET中大文件下载的跟踪和恢复
- 在ASP.NET中支持断点续传下载大文件(ZT)
- Asp.net(c#)实现多线程断点续传下载大文件
- 在ASP.NET中支持断点续传下载大文件(ZT)
- recovery: ASCII cpio archive (SVR4 with no CRC)——cpio格式的recovery解压缩和重新打包的方
- [绍棠] 学习网站
- 背包之01背包、完全背包、多重背包详解
- cocos2dx中使用自定义uniform变量
- android sdcard目录chmod无法生效问题
- sivelrlight 结合asp.net用于大文件下载
- 2016夏季练习——搜索
- Android IntentService使用全面介绍及源码解析
- 【JZOJ4586】Ned 的难题
- Android js和native交互的两种方法
- PTA 朋友圈【并查集的合并问题】
- 基础知识(十四)服务器搭建
- java动态加载
- 树的子结构