ASP.NET 中,实现download下载,弹出打开和保存对话框,不限制文件大小,跪求实现代码,谢谢了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET 中,实现download下载,弹出打开和保存对话框,不限制文件大小,跪求实现代码,谢谢了相关的知识,希望对你有一定的参考价值。
昨天的那位大侠,再次麻烦你了,谢谢
思路很简单,读取服务器文件路径,然后再保存数据流,下面是实现代码:(ps:因为要上班,来不及写很多注释,关键的地方加了几句注释哈)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.htmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using YTLib.Basic;
using YTLib.YTDBC;
using System.IO;
using System.Threading;
namespace siteadmin.admin
public partial class downfile : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
string strSQL = "";
YTNDBObject dbo = new YTNDBObject();
dbo.DataName = "Main";
Page.EnableViewState = false;
if(!IsPostBack)
string sid=Request["id"];
if(!YTLib.publicOP.IsNumString(sid)||sid==null)
sid="0";
strSQL = "select * from FileSource where ID=@ID";
dbo.PrepareCommand(strSQL);
dbo.SetCmdIntValue("@ID",int.Parse(sid));
DataTable dt=dbo.QueryData().Tables[0];
string sourceName = (string)dt.Rows[0]["FilePath"];
sourceName.Trim();//消除空格
string filesnames = sourceName.Substring(7, sourceName.Length - 8);
int index= sourceName.LastIndexOf(".");
string extend = sourceName.Substring(index+1);//扩展名
string fullPath = "~/uploaded/" + sourceName;
fullPath = Server.MapPath(fullPath);
//读出该资源的下载次数
int downloadtimes = 0;
downloadtimes = int.Parse(dt.Rows[0]["downloadCounts"].ToString());
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, filesnames, fullPath, 1024000);
if (!success) Response.Write("<script language=\"javascript\">alert(\"Download file error\");window.location.href=\"../Download.aspx\"</script>");
else
//记录下载次数
downloadtimes++;
string sqlStr = "update FileSource set downloadCounts=" + downloadtimes + " where ID=@IID";
dbo.PrepareCommand(sqlStr);
dbo.SetCmdIntValue("@IID",int.Parse(sid));
dbo.ExecuteNoQuery();
Response.Write("<script language=\"javascript\">window.location.href=\"../Download.aspx\"</script>");
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
try
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;//不缓冲
long fileLength = myFile.Length;
long startBytes = 0;
double pack = 10240;
//10K bytes
int sleep = 200;
//每秒5次 即5*10K bytes每秒 int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] '=', '-' );
startBytes = Convert.ToInt64(range[1]);
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
//Response.AddHeader("Content-Range", string.Format(" bytes 0-1/2", startBytes, fileLength-1, fileLength));
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
if (_Response.IsClientConnected)
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
else
i = maxCount;
catch
return false;
finally
br.Close();
myFile.Close();
catch
return false;
return true;
参考技术A 本地下载 直接链接压缩包的路径就可以。
要是用迅雷下载,到迅雷官网上找,有专门的链接代码、。
asp.net中GridView怎样进行分页,编辑,删除操作
用 GridView 做个显示数据的,想直接在控件上进行这些操作,应该要怎样写啊
参考技术A 自已从下载的资料中下载示例看吧,示例包含如下内容:GridView无代码分页排序
GridView选中,编辑,取消,删除
GridView正反双向排序
GridView和下拉菜单DropDownList结合
GridView和CheckBox结合
鼠标移到GridView某一行时改变该行的背景色方法一
鼠标移到GridView某一行时改变该行的背景色方法二
GridView实现删除时弹出确认对话框
GridView实现自动编号
GridView实现自定义时间货币等字符串格式
GridView实现用“...”代替超长字符串
GridView一般换行与强制换行
GridView显示隐藏某一列
GridView弹出新页面/弹出新窗口
GridView固定表头(不用javascript只用CSS,2行代码,很好用)
GridView合并表头多重表头无错完美版(以合并3列3行举例)
GridView突出显示某一单元格(例如金额低于多少,分数不及格等)
GridView加入自动求和求平均值小计
GridView数据导入Excel/Excel数据读入GridView
以上是关于ASP.NET 中,实现download下载,弹出打开和保存对话框,不限制文件大小,跪求实现代码,谢谢了的主要内容,如果未能解决你的问题,请参考以下文章
怎么实现在ASP.NET点击一个按钮就弹出要用户输入(含ASP.NET组件)的窗体?
asp.net中GridView怎样进行分页,编辑,删除操作