private void Page_Load(object sender, System.EventArgs e)
{
string fPath=Request.QueryString["fonlyName"];
string fileName=fPath;
string filePath=Server.MapPath(fPath);
Response.Clear();
Response.ClearHeaders();
Response.Charset="utf-8";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType="application/octet-stream";
FileInfo fi = new FileInfo(filePath);
Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName));
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tempbyte = new byte[ 1024*8];
FileStream fs = fi.OpenRead();
int count;
while((count = fs.Read(tempbyte,0,tempbyte.Length))>0)
{
Response.BinaryWrite(tempbyte);
Response.Flush();
}
fs.Close();
Response.End();
}
要连接这个页面的是文件列举的页,传过来的值是数据库中文件的一个唯一名子,文件是直接放在我的服务器根目录里的,
这段代码是dl.aspx页面的全部内容.传过来的文件名通过string filePath=Server.MapPath(fPath);得到文件在服务器里的路径.
执行后没有报错,奇怪的是在也面列举页中点击文件名后有的文件可以下载,有的文件不能下载(此时报错为路径中具有非法字符,string filePath=Server.MapPath(fPath);这句话被加红颜色)
而且有时是出现windows下载提示,而有时就直接打开了文件出现一些乱码,十分的不稳定.
请帮忙修改下这个程序或者提供更好点的下载方法.
就要毕业答辩了,不能让偶去 找些可以下载下来的文件来骗老师们吧!在此先谢过了,请各位帮忙,急急急!
{
string fPath=Request.QueryString["fonlyName"];
string fileName=fPath;
string filePath=Server.MapPath(fPath);
Response.Clear();
Response.ClearHeaders();
Response.Charset="utf-8";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType="application/octet-stream";
FileInfo fi = new FileInfo(filePath);
Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName));
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tempbyte = new byte[ 1024*8];
FileStream fs = fi.OpenRead();
int count;
while((count = fs.Read(tempbyte,0,tempbyte.Length))>0)
{
Response.BinaryWrite(tempbyte);
Response.Flush();
}
fs.Close();
Response.End();
}
要连接这个页面的是文件列举的页,传过来的值是数据库中文件的一个唯一名子,文件是直接放在我的服务器根目录里的,
这段代码是dl.aspx页面的全部内容.传过来的文件名通过string filePath=Server.MapPath(fPath);得到文件在服务器里的路径.
执行后没有报错,奇怪的是在也面列举页中点击文件名后有的文件可以下载,有的文件不能下载(此时报错为路径中具有非法字符,string filePath=Server.MapPath(fPath);这句话被加红颜色)
而且有时是出现windows下载提示,而有时就直接打开了文件出现一些乱码,十分的不稳定.
请帮忙修改下这个程序或者提供更好点的下载方法.
就要毕业答辩了,不能让偶去 找些可以下载下来的文件来骗老师们吧!在此先谢过了,请各位帮忙,急急急!
解决方案 »
- 【DIV弹出层】求在Asp.Net下好用的DIV弹出层...
- 数据库的一点问题
- Asp.net 缓存问题? 急急急!
- 初级问题,页面可以传的查询字符串最多可以有多长?大于255行么?
- 下载输出代码为什么时常出问题?代码如下
- 学着用Ajaxpro弄了半天也没解决
- 求教高手....
- ascx 继承的问题 :怎么实现 页面上的一些东西 的继承
- 什么情况下用 class 或者 sub
- 查看网站中WEB服务中的方法时报异常
- WebForm1.asp*的HTML页面有什么作用呢?用WebForm1.asp*的设计页面和WebForm1.asp.cs代码页面就可以完成功能了,为什么还要HTML页面呢?
- 取 TreeView 控件的值.
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"></param>
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
2. vb.net
Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
Try
If File.Exists(page.MapPath(strFileName)) Then
Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
'清除缓冲区流中的所有内容输出.
page.Response.Clear()
'向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))
'繁体格式
'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8)) '向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
'表明输出的HTTP为流[stream],因此客户端只能下载.
page.Response.ContentType = "application/octet-stream"
'发送文件流到客户端.
page.Response.WriteFile(TargetFile.FullName)
'停止执行当前页
page.Response.End()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
行 15: dim msg_id as integer = request.QueryString("id")
行 16: response.writefile("msg.aspx?type=" & msg_type & "&id=" & msg_id)
行 17: response.End()
行 18: end if
路径中具有非法字符