主题:救急:关于文件操作的问题!!!!!!!!
目的:将需要上传的文件以及其他一些信息,重新写入一个新文件。
代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
PackFile myPackFile;
myPackFile.Title = "测试的例子";
myPackFile.Name = "我想对世界说:";
string myStr = "Hello World!";
//向文件中写入文件属性
FileStream sb = new FileStream("d:\\wwwroot\\WebApplication1\\file\\test", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(sb);
sw.Write(myPackFile.Title.Length);
sw.WriteLine(myPackFile.Title);
sw.Write(myPackFile.Name.Length);
sw.WriteLine(myPackFile.Name);
sw.Write(myStr.Length);
sw.WriteLine(myStr);
//将附件内容写入文件
//附件名称
string tempFileName = this.File1.PostedFile.FileName;
string[] filename = this.File1.PostedFile.FileName.Split(new char[] {'\\'});
string upfilename = "";
upfilename = filename[filename.Length-1];
//文件类型
string FileType = this.File1.PostedFile.ContentType;
//附件大小
sw.WriteLine(FileType);
int upfilelengh=this.File1.PostedFile.ContentLength;
//设置文件流存放数组
char[] arrfile=new char[upfilelengh]; //将文件读入数组
StreamReader sr = new StreamReader(tempFileName);
sr.Read(arrfile, 0, upfilelengh);
byte[] byData = new byte[arrfile.Length];
Stream StreamReader = this.File1.PostedFile.InputStream;
StreamReader.Read(byData, 0, byData.Length);
Decoder mydecoder = Encoding.UTF8.GetDecoder();
mydecoder.GetChars(byData,0,byData.Length,arrfile,0);
sw.Write(arrfile);
sr.Close();
sw.Close();
sb.Close();
}
执行结果:1,若读入压缩包,则报错:在索引 204 处发现无前置高代理项的低代理项字符。输入可能不在此编码内,或者可能不包含有效的 Unicode (UTF-16)字符。参数名: chars
2、若读入WORD文档,用文本文件查看test时,则上传的文件显示乱码。
疑问:上述代码是否存在问题,生产一个文件时是否需要甚么代码的转换??
代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
PackFile myPackFile;
myPackFile.Title = "测试的例子";
myPackFile.Name = "我想对世界说:";
string myStr = "Hello World!";
//向文件中写入文件属性
FileStream sb = new FileStream("d:\\wwwroot\\WebApplication1\\file\\test", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(sb);
sw.Write(myPackFile.Title.Length);
sw.WriteLine(myPackFile.Title);
sw.Write(myPackFile.Name.Length);
sw.WriteLine(myPackFile.Name);
sw.Write(myStr.Length);
sw.WriteLine(myStr);
//将附件内容写入文件
//附件名称
string tempFileName = this.File1.PostedFile.FileName;
string[] filename = this.File1.PostedFile.FileName.Split(new char[] {'\\'});
string upfilename = "";
upfilename = filename[filename.Length-1];
//文件类型
string FileType = this.File1.PostedFile.ContentType;
//附件大小
sw.WriteLine(FileType);
int upfilelengh=this.File1.PostedFile.ContentLength;
//设置文件流存放数组
char[] arrfile=new char[upfilelengh]; //将文件读入数组
StreamReader sr = new StreamReader(tempFileName);
sr.Read(arrfile, 0, upfilelengh);
byte[] byData = new byte[arrfile.Length];
Stream StreamReader = this.File1.PostedFile.InputStream;
StreamReader.Read(byData, 0, byData.Length);
Decoder mydecoder = Encoding.UTF8.GetDecoder();
mydecoder.GetChars(byData,0,byData.Length,arrfile,0);
sw.Write(arrfile);
sr.Close();
sw.Close();
sb.Close();
}
执行结果:1,若读入压缩包,则报错:在索引 204 处发现无前置高代理项的低代理项字符。输入可能不在此编码内,或者可能不包含有效的 Unicode (UTF-16)字符。参数名: chars
2、若读入WORD文档,用文本文件查看test时,则上传的文件显示乱码。
疑问:上述代码是否存在问题,生产一个文件时是否需要甚么代码的转换??