C#文件方法-飞外

一:TransmitFile文件,不需要打开关闭文件,直接插入传输流中

Response.ContentType="application/x-zip-compressed";

Response.AddHeader("Content-Disposition",

"attachment;filename=z.zip");

stringfilename=Server.MapPath("DownLoad/z.zip");

Response.TransmitFile(filename);

二:


客户端保存的文件名

stringfilePath=Server.MapPath("DownLoad/aaa.txt");//路径

FileInfofileInfo=newFileInfo(filePath);

Response.Clear();

Response.ClearContent();

Response.ClearHeaders();

Response.AddHeader("Content-Disposition","attachment;fileContent-Length",fileInfo.Length.ToString());

Response.AddHeader("Content-Transfer-Encoding","binary");

Response.ContentType="application/octet-stream";

Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");

Response.WriteFile(fileInfo.FullName);

Response.Flush();

Response.End();

三:流方式

FileStreamfs=newFileStream(filePath,FileMode.Open);

byte[]bytes=newbyte[(int)fs.Length];

fs.Read(bytes,0,bytes.Length);

fs.Close();

Response.ContentType="application/octet-stream";//通知浏览器文件而不是打开

Response.AddHeader("Content-Disposition","attachment; filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));

Response.BinaryWrite(bytes);

Response.Flush();

Response.End();