谁有,发个过来啊,要任何客户端txt都能够下载的
解决方案 »
- 求.net类库源码
- c# 根据选择的文件类型直接打开
- 如何在iframe里实现excel下载??
- ¥¥¥¥¥ 点击页面的某按钮后,如何用jscript实现:打开一个含有textbox的小窗口,用户输完值后,将此值传回页面
- 请问:如何把textarea中的内容以txt的格式保存起来
- ”题库“英文怎么翻译,50分寻求答案
- 请教一个ListBox控件的简单问题
- fileupload控件在修改的时候路径是否可以显示
- 加了RegularExpressionValidator控件,但是需要必须不为空,但是我希望能够用户可以不填
- 关于Link Server查询的问题!请高手指点一下啊!
- 关于日历控件,求指导
- RDLC 数字显示格式求助!
/// 下载文件
/// </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);
}
}
{
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?[参数名]=";
/// 下载文件
/// </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();
}