请问:在文件下载时,点击打开,文件名为乱码,(已经进行过编码),如何解决?

luyes 2006-12-12 08:57:15
在文件下载时,点击打开,文件名为乱码????(已经进行过编码)

Dim filename As String
filename = tbFilepath.Text
'获得文件的扩展名
Dim fileext As String
Dim i As Integer
i = filename.LastIndexOf(".")
fileext = filename.Substring(i, filename.Length - i)
'获得文件的路径
Dim filepath As String
'找到文件的物理路径
filepath = Server.MapPath("./") + filename

'实例化文件流对象
Dim fs As FileStream
fs = New FileStream(filepath, FileMode.Open)
'获取文件流的大小
Dim fsize As Long
fsize = Convert.ToInt32(fs.Length)
'给数组赋值
Dim mybyte() As Byte
ReDim mybyte(fsize)
'将文件的数据读入数组
fs.Read(mybyte, 0, fsize)
fs.Close()
'以二进制流的形式读到浏览器
Response.AddHeader("Content-Disposition", "attachment;filename=" & System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8))
Response.ContentType = "application/octet-stream"
Response.BinaryWrite(mybyte)
Response.Flush()
Response.Close()
Response.End()

用这段代码实现了的文件的下载,然后在弹出的对话框中,点击下载的话,文件名就和原文件名一致,但是如果你点击直接打开,文件名就变成了乱码,这是中文文件名;如果是数字文件名,如11.doc,就变成了11[1].doc,请问高手:如何在打开文件的时候,让文件名也是正确的??
此前有人问过类似问题(http://community.csdn.net/Expert/TopicView3.asp?id=4956326),但没有解决。在线等...
...全文
485 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyx_gritty 2007-02-01
  • 打赏
  • 举报
回复
好象这个问题没有办法解决.....
因为你打开的文件是在客户端创建的文件.这个文件就是在你在文件下载对话框中点击打开文件时创建的,而文件名是经过System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8))编码,但是没有经过反编码.它是一段System.Text.Encoding.UTF8编码的字符串.所以
wyx_gritty 2007-02-01
  • 打赏
  • 举报
回复
luyes(虚心请教者) : 我也遇到了跟你一样的问题.顶!!
magicmaster 2006-12-12
  • 打赏
  • 举报
回复
我就碰到过utf8/7 gb2312 乱码 default解决这种要命的情况
magicmaster 2006-12-12
  • 打赏
  • 举报
回复
Response.AddHeader("Content-Disposition", "attachment;filename=" & System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8))


utf8 utf7 default都试试
hy_lihuan 2006-12-12
  • 打赏
  • 举报
回复
你看一下是不是你本机的temp里面已经有这个文件的缘故阿?所以会变成11[1].txt
zdywqy 2006-12-12
  • 打赏
  • 举报
回复
up
wen7679 2006-12-12
  • 打赏
  • 举报
回复
Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)或Response.AddHeader("Content-Disposition", "attachment;filename=" & System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.GetEncoding("gb2312")))
palts520 2006-12-12
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Net;
using System.Text;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Sockets;
using System.Text.RegularExpressions;

namespace SZCS
{
/// <summary>
/// downFile 的摘要说明。
/// </summary>
public class downFile : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

WebClient myclient=new WebClient();
string strFile="C:\\Documents and Settings\\Administrator\\桌面\\aspjpeg.exe"; //服务器上要共下载的文件
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment;filename=" +
System.Web.HttpUtility.UrlEncode(strFile)) ;

Response.WriteFile(strFile);

if(!System.IO.File.Exists(strFile))
{
Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
FileInfo fi=new FileInfo(strFile);
Response.AddHeader("Content-Disposition","attachment; filename=" + System.Web.HttpUtility.UrlEncode(fi.Name)) ;
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tmpbyte=new byte[1024*8];
FileStream fs=fi.OpenRead();
int count;
while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0)
{
Response.BinaryWrite(tmpbyte);
Response.Flush();
}
fs.Close();
Response.End();

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
sunjay117 2006-12-12
  • 打赏
  • 举报
回复
应该跟你的webconfig一致把
heartdevil 2006-12-12
  • 打赏
  • 举报
回复
public void DownLoadFiles(string fullName)
{
// retrieve the path of the file to download, and create
// a FileInfo object to read its properties
string path = Server.MapPath("../Files/"+fullName);
System.IO.FileInfo file = new System.IO.FileInfo(path);
string subName = file.Name.Substring(14);
string fileName = subName.Substring(0,subName.LastIndexOf("."));
string Ext = subName.Substring(subName.LastIndexOf("."));
// clear the current output content from the buffer
Response.Clear();
// add the header that specifies the default filename for the Download/SaveAs dialog

//设置此句,防止下载对话框中的选项在中文标题时,出现乱码。
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName)+ Ext);
// add the header that specifies the file size, so that the browser
// can show the download progress

//设置为中文,防止乱码,相当于Response.AddHeader("Content-type", "text/html;Charset=GB2312");
Response.Charset = "GB2312";
//设置输出流为简体中文输出流。
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");

Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/octet-stream";
// send the file stream to the client
Response.WriteFile(file.FullName);
// stop the execution of this page
Response.End();
}
jsyhello 2006-12-12
  • 打赏
  • 举报
回复
Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)或Response.AddHeader("Content-Disposition", "attachment;filename=" & System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.GetEncoding("gb2312")))

hegang888888888 2006-12-12
  • 打赏
  • 举报
回复
UTF8改为gb2312试一试
weiqiaoxyz 2006-12-12
  • 打赏
  • 举报
回复
看看
WeekZero 2006-12-12
  • 打赏
  • 举报
回复
我遇到过导出到excel中,编码也是已经改好了,并且同样的代码别的没有问题

后来发现是因为那个文件中的数据问题
luyes 2006-12-12
  • 打赏
  • 举报
回复
谢谢各位,但还是不行啊!

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧