zoukankan      html  css  js  c++  java
  • .Net 无法打开Offie Open XML文件(上传和下载使用)

      代码:

    首先,贴一个上传

     1 public static string UploadFileIntoDir(FileUpload MyFile, string DirName)
     2         {
     3             if (IfOkFile(DirName) == true)
     4             {
     5                 string ReturnStr = string.Empty;               
     6                 if (MyFile.FileContent.Length > 0)
     7                 {
     8                     MyFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../UploadFile/") + DirName);
     9                     //将原文件名与现在文件名写入ERPSaveFileName表中
    10                     //string NowName = DirName;
    11                     //修改下载文件换成源文件名称 + 时间节
    12                     string NowName = DirName;
    13                     string OldName = MyFile.FileName;
    14                     string SqlTempStr = "insert into ERPSaveFileName(NowName,OldName) values ('" + NowName + "','" + OldName + "')";
    15                     ZWL.DBUtility.DbHelperSQL.ExecuteSQL(SqlTempStr);
    16                     return OldName;
    17                 }
    18                 else
    19                 {
    20                     return ReturnStr;
    21                 }
    22             }
    23             else
    24             {
    25                 if (MyFile.FileName.Length > 0)
    26                 {
    27                     System.Web.HttpContext.Current.Response.Write("<script>alert('不允许上传此类型文件!');</script>");
    28                     return "";
    29                 }
    30                 else
    31                 {
    32                     return "";
    33                 }
    34             }
    35         }
    View Code

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

      然后,贴一个下载代码:

     1         System.IO.Stream iStream = null;
     2         byte[] buffer = new Byte[100000];
     3         int length;
     4         long dataToRead;
     5         string filepath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
     6         string filename = System.IO.Path.GetFileName(filepath);
     7         try
     8         {
     9             iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
    10             dataToRead = iStream.Length;
    11             Response.ContentType = "application/octet-stream";
    12             Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName));
    13             while (dataToRead > 0)
    14             {
    15                 if (Response.IsClientConnected)
    16                 {
    17                     length = iStream.Read(buffer, 0, 100000);
    18                     Response.OutputStream.Write(buffer, 0, length);
    19                     Response.Flush();
    20                     buffer = new Byte[100000];
    21                     dataToRead = dataToRead - length;
    22                 }
    23                 else
    24                 {
    25                     dataToRead = -1;
    26                 }
    27             }
    28         }
    29         catch (Exception ex)
    30         {
    31             string message = ex.Message;
    32             this.Page.ClientScript.RegisterStartupScript(GetType(), "Message", "<script>alert('Error : " + message + "');</script>");
    33         }
    34         finally
    35         {
    36             if (iStream != null)
    37             {
    38                 iStream.Dispose();
    39             }
    40         }
    View Code

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

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

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

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

    请使用 using{} 

     1 string filepath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
     2         string filename = System.IO.Path.GetFileName(FilePath);
     3         using (var iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
     4                     System.IO.FileAccess.Read, System.IO.FileShare.Read))
     5         {
     6             Response.Clear();
     7             Response.ContentEncoding = System.Text.Encoding.UTF8;
     8             Response.ContentType = "application/octet-stream";
     9             Response.AddHeader("Content-Disposition", "attachment; filename=" +
    10                 System.Web.HttpUtility.UrlEncode(FileName));
    11             iStream.CopyTo(Response.OutputStream);
    12             Response.End();
    13         }
    View Code

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

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

  • 相关阅读:
    艾伦 Visual Studio 批量自动化代码操作工具-VS插件发布
    Visual Studio 打开解决方案后 弹出框显示 "正在打开文件..." 迟迟没反应 的解决方法
    小米抢购神器-开放源码
    python语法
    python运算符
    python字符串
    python多线程,多进程编程。
    subprocess模块
    jenkins
    python中的lxml模块
  • 原文地址:https://www.cnblogs.com/JeffController/p/4330343.html
Copyright © 2011-2022 走看看