读书人

求简单的ASP.NET上载代码

发布时间: 2013-01-12 16:25:03 作者: rapoo

求简单的ASP.NET下载代码
谁有,发个过来啊,要任何客户端txt都能够下载的
[解决办法]
/// <summary>
/// 下载文件
/// </summary>
protected void Down_Click(object sender, EventArgs e)
{
//获取id集合
string ids = Utils.GetRequest("cbItem");
if (ids == string.Empty)
{
return;//未选择任何文件
}
List<int> list = Utils.Getids(ids);
FileBLL filebll = new FileBLL();
//下载选中文件
foreach (int id in list)
{
Down(filebll.GetFile(id).FileAddr);
}
}
[解决办法]
if (File.Exists(fileName))
{
Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
long num3 = stream.Length - 1L;
HttpContext.Current.Response.AddHeader("Content-Range", "bytes 0-" + num3.ToString() + "/" + stream.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Length", stream.Length.ToString());
if (!isPreview)
{
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + displayName);
}
long length = stream.Length;


byte[] buffer = new byte[block];
while (length > 0L)
{
if (HttpContext.Current.Response.IsClientConnected)
{
int count = stream.Read(buffer, 0, block);
HttpContext.Current.Response.OutputStream.Write(buffer, 0, count);
HttpContext.Current.Response.Flush();
buffer = new byte[block];
length -= count;
}
else
{
length = -1L;
}
}
stream.Close();
HttpContext.Current.Response.End();
}
else
{
HttpContext.Current.Response.Write("file no found");
}
[解决办法]
模拟浏览器下载(点击文件,弹出保存/下载框)
一:建ashx文件XXX.ashx,包含下面两个方法:

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
// 获取要下载的内容 byte[]类型(支持各种类型文件哦)
var param= context.Request["参数名"];
var content = "";
// 执行下载
DownLoad(context, content, sendMessage.AttachName);


}

private static void DownLoad(HttpContext context, byte[] content, string fileName)
{
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "标题; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(content);
context.Response.Flush();
context.Response.End();
}
二:js code
点击文件
window.location.href = "XXX.ashx?[参数名]=";

[解决办法]
这里有很多。。。。。 自己去下载。。

http://www.51aspx.com
[解决办法]

引用:
引用:引用:/// <summary>
/// 下载文件
/// </summary>
protected void Down_Click(object sender, EventArgs e)
{
……

/// <summary>
/// 下载文件
/// </summary>
protected void Down_Click(object sender, EventArgs e)
{
//获取id集合
string ids = Utils.GetRequest("cbItem");
if (ids == string.Empty)
{
return;//未选择任何文件
}
List<int> list = Utils.Getids(ids);
FileBLL filebll = new FileBLL();
//下载选中文件
foreach (int id in list)
{
Down(filebll.GetFile(id).FileAddr);
}
}

/// <summary>
/// 文件下载
/// </summary>


/// <param name="FilePath">文件路径</param>
private void Down(string FilePath)
{
String FullFileName = System.Web.HttpContext.Current.Server.MapPath(FilePath);
FileInfo DownloadFile = new FileInfo(FullFileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}

读书人网 >asp.net

热点推荐