解决方案 »
- 三个表查询的SQL语句
- 关于商品过期问题--有一个条件怎么写??
- 高手请进,弹出提示弹不出来,昨天还是好好的今天打开就出错,郁闷
- 如何获取POST过来的所有值?
- 为什么我建一个asp.net的项目,哪怕里面什么都没有,用VS打开浏览器的进度条一直停在那,没提示任何错误
- LinkButton奇怪问题
- 下拉框一条记录内容比较长,不改变下拉框宽度,有没有办法让用户看到全部的字啊?
- 如何让同一个用户控件在不同的页面显示在不同的位置?
- 请问如何在.HTML页面中接受参数并传到另一个.aspx页面中
- 向服务器端提交XML字符串时出现的问题,跟Request.Form[..]有关(高分,不够再加)
- ASP.NET Htmlencode转换写入数据库
- 急用!! 代码优化、、
http://www.cnblogs.com/insus/articles/1411761.html
http://www.cnblogs.com/insus/articles/2003336.html
DataRow objDataRow = objMedia.GetFileByPrimaryKey(id).Rows[0];
objMedia和id是什么?
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;namespace Asiastar.NR.Ajax
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"; string id = context.Request["id"].ToString();//获取资源的编号
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
NRBLL.File bf = new Asiastar.NRBLL.File();
Guid guid = new Guid(id);
if (bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"] != null)//判断数据库路径是否存在
{
string filepath = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径 D:\资源文件\600cc139-14cf-448e-9e50-daa972d35e01.jpg
string Oidfilename = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FileNam"].ToString();//旧文件名称
//string filename = System.IO.Path.GetFileName(filepath);//获取文件名称+后缀名 600cc139-14cf-448e-9e50-daa972d35e01.JPG
//int index = filepath.IndexOf(".");
//string filetype = filepath.Substring(index).ToLower();//后缀名
//string newfilename = Oidfilename;
//string filepath1 = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString().Substring(0,filepath.Length - 8);
try
{
string fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Oidfilename));//解码(注意这里2层解码)
Oidfilename = Oidfilename.Replace("+", "%20"); //将“+”替换成“空格”
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Oidfilename, System.Text.Encoding.UTF8)); //下载的时候下载原来的文件名称
while (dataToRead > 0)
{
if (context.Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000);
context.Response.OutputStream.Write(buffer, 0, length);
context.Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
} }
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
}
else
{
NR.Error.Log.LogType("找不到文件!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
public static void DownLoadFile(string file)
{
HttpContext.Current.Response.Clear();
System.IO.FileInfo finfo = new System.IO.FileInfo(file);
if (!finfo.Exists)
{
oJavaScript.Alert("文件不存在");
return;
}
else
{
string reportfile = finfo.FullName;
string filename = finfo.Name;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8));
HttpContext.Current.Response.WriteFile(reportfile);
HttpContext.Current.Response.End();
}
}
上面的代码,我这里可以
Sys.WebForms.PageRequestManagerParserErrorExeception:从服务器收到的消息不能被解析。这种错误的常见原因是响应时调用细节修改:误差解析附近'□ □ □ □
大功告成!分享下:
我建了个一般处理程序,代码如下:
public class FileDownLoad : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
System.IO.FileInfo file = new System.IO.FileInfo(context.Request["filepath"].ToString()); context.Response.ContentType = "image/pic/pdf/bmp/png/gif/jpg/swf"; //例: "image/gif" context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(file.Name) + "\""); context.Response.TransmitFile(System.Web.Hosting.HostingEnvironment.MapPath(context.Request["filepath"].ToString())); //常用方法, 大文件需换成Stream分段下载 } public bool IsReusable
{
get
{
return true;
}
}
}
然后在下载事件中写如下代码:
Response.Redirect("FileDownLoad.ashx?filepath="+filePath);“FileDownLoad.ashx”为我创建的一般处理程序页面,“filePath”为传过去的虚拟路径
跟三楼的差不多,谢谢各位了啊……