.Net 无法打开Offie Open XML文件(上传和下载使用)
2024-09-02 04:20:08
代码:
首先,贴一个上传
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,请点击此处
最新文章
- 再探OAuth2
- 【Unity】改变向量的方向而不改变其大小
- TestNg依赖高级用法之强制依赖与顺序依赖------TestNg依赖详解(二)
- Asp.net磁力链接搜索引擎源码-www.btboot.com
- Visual Studio 2012 破解版
- mysql deadlock处理
- 详解Google Chrome浏览器(操作篇)(一)
- jquery、js获取页面高度宽度等
- AndroidDevTools
- 使用WSL吧
- Runnable和Callable之间的区别
- 期末Java Web大作业----简易的学生管理系统
- Springboot 4.Springboot 集成SwaggerUi
- self sqflite sample =======================
- 谷歌浏览器javascript错误提示插件
- nvidia-smi 命令解读
- 12_Python操作MySQL(basic)
- css文字环绕
- fork系统炸弹
- Linux ->;>; <;user_name>; not in the sudoers file. This incident will be reported.