代码:

首先,贴一个上传

 public static string UploadFileIntoDir(FileUpload MyFile, string DirName)
{
if (IfOkFile(DirName) == true)
{
string ReturnStr = string.Empty;
if (MyFile.FileContent.Length > )
{
MyFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../UploadFile/") + DirName);
//将原文件名与现在文件名写入ERPSaveFileName表中
//string NowName = DirName;
//修改下载文件换成源文件名称 + 时间节
string NowName = DirName;
string OldName = MyFile.FileName;
string SqlTempStr = "insert into ERPSaveFileName(NowName,OldName) values ('" + NowName + "','" + OldName + "')";
ZWL.DBUtility.DbHelperSQL.ExecuteSQL(SqlTempStr);
return OldName;
}
else
{
return ReturnStr;
}
}
else
{
if (MyFile.FileName.Length > )
{
System.Web.HttpContext.Current.Response.Write("<script>alert('不允许上传此类型文件!');</script>");
return "";
}
else
{
return "";
}
}
}

  这里的意思就是上传一个简单的文件到服务器,就是到相对于项目路径的文件夹下面。

  然后,贴一个下载代码:

         System.IO.Stream iStream = null;
byte[] buffer = new Byte[];
int length;
long dataToRead;
string filepath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
string filename = System.IO.Path.GetFileName(filepath);
try
{
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName));
while (dataToRead > )
{
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, , );
Response.OutputStream.Write(buffer, , length);
Response.Flush();
buffer = new Byte[];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -;
}
}
}
catch (Exception ex)
{
string message = ex.Message;
this.Page.ClientScript.RegisterStartupScript(GetType(), "Message", "<script>alert('Error : " + message + "');</script>");
}
finally
{
if (iStream != null)
{
iStream.Dispose();
}
}

  这里的代码之所以这么写是因为需要流处理数据。

  顺便给大家一个小小的下载提示:我们保存文件的时候害怕文件同名会用DateTime.Now.Ticks来防止文件名重复,但是下载的时候就有一个苦恼了,大家看着一大推的数字,根本分不清楚到底哪个是哪个,那么,现在可以建立一个独立的页面,然后,这个页面就只有这个方法,就像二进制图片的我们要释放那样,首先建立一个独立的页面,然后从后台的二进制传递上来的时候去接收,然后再另外的页面放img标签,把这个二进制的页面给放进去,就是这样,我们就可以给文件任意命名了。

  到了主题了,主题就是这里的下载有问题,03和03以下的office组件可以任意,但是07的组件,因为存在备份文件(特别是docx),下载之后打开的时候会出现

小伙伴们千万不要着急,这个问题我也遇到了(这是点击确定,然后继续点击是,还是可以得到原来的信息的么),通过两天的纠结,问题的根找出来了,就是流搞的鬼,因为这里的流虽然finally释放了,但是因为备份,所以释放的并不完整,那么

请使用 using{}

 string filepath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
string filename = System.IO.Path.GetFileName(FilePath);
using (var iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read))
{
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" +
System.Web.HttpUtility.UrlEncode(FileName));
iStream.CopyTo(Response.OutputStream);
Response.End();
}

还是.Net Framework设计的好呀,直接using,把人家发现的内存直接释放,完美解决

至于平时的文件说的 无法打开 Office Open Xml,请点击此处

最新文章

  1. 再探OAuth2
  2. 【Unity】改变向量的方向而不改变其大小
  3. TestNg依赖高级用法之强制依赖与顺序依赖------TestNg依赖详解(二)
  4. Asp.net磁力链接搜索引擎源码-www.btboot.com
  5. Visual Studio 2012 破解版
  6. mysql deadlock处理
  7. 详解Google Chrome浏览器(操作篇)(一)
  8. jquery、js获取页面高度宽度等
  9. AndroidDevTools
  10. 使用WSL吧
  11. Runnable和Callable之间的区别
  12. 期末Java Web大作业----简易的学生管理系统
  13. Springboot 4.Springboot 集成SwaggerUi
  14. self sqflite sample =======================
  15. 谷歌浏览器javascript错误提示插件
  16. nvidia-smi 命令解读
  17. 12_Python操作MySQL(basic)
  18. css文字环绕
  19. fork系统炸弹
  20. Linux -&gt;&gt; &lt;user_name&gt; not in the sudoers file. This incident will be reported.

热门文章

  1. Clang教程之实现源源变化
  2. xml_dom解析
  3. deep_learning_Function_bath_normalization()
  4. WinRAR去广告
  5. SpringCloud 第一篇:服务的注册和发现(Eureka)
  6. Linux umask 档案预设权限/touch 建立空档案或修档案件时间
  7. Jenkins 入门系列--Jenkins 的安装配置
  8. 牛客练习赛46 A 华华教奕奕写几何 (简单数学)
  9. supdf
  10. 数组与pandas模块