上传文件

aspxbbs123 2009-04-10 02:29:07

想把一个.doc文件或.xls文件存放到数据库.
在vs2005运行后能够把 .doc文件或.xls文件存放到数据库.但是发布到服务器上去后,有些文件能够存放到数据库,有些则出错.但我把存放进数据库出错的那个文件在 vs2005里面运行,是可以存放进数据库. 关键是发布后有些可以,有些不可以,真的很郁闷,哪位高手帮帮我?
出错代码:
Server Error in '/' Application.
Cannot access a closed file.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ObjectDisposedException: Cannot access a closed file.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
...全文
103 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2009-04-13
  • 打赏
  • 举报
回复
你再看看 能上传的和不能上传的 有什么差别
aspxbbs123 2009-04-13
  • 打赏
  • 举报
回复
3楼同志的回答:
本機和服務的路徑是不一樣的吧。 看看路徑是否一至。。。
4楼同志的回答:
不能操作关闭文件 路径是否能操作,权限可够。
5楼同志的回答故障依然.

在这里我有点不明白:
为什么发布到服务器后只有小部分文件会出错啊?应该全部出错才对啊!

现在搞得我 有部分文件由客户上传,有部分文件要通过我打开 vs2005运行后来上传.真的好晕啊!

hefuming5 2009-04-10
  • 打赏
  • 举报
回复
虽然看不太懂但还是帮顶下
levinstong 2009-04-10
  • 打赏
  • 举报
回复
阿非 2009-04-10
  • 打赏
  • 举报
回复

SqlConnection con = new SqlConnection("server=200.0.0.1;database=Temp;uid=sa;pwd=sa");
con.Open();
HttpFileCollection HFC = HttpContext.Current.Request.Files;
HttpPostedFile UserHPF;
System.IO.Stream fs ;
StringBuilder strSql ;
for (int i = 0; i < HFC.Count; i++)
{
UserHPF = HFC[i];

if (UserHPF.ContentLength > 0)
{
fs = UserHPF.InputStream;
strSql = new StringBuilder();
strSql.Append("insert into FileSaveTemp(");
strSql.Append("DocId,Content)");
strSql.Append(" values (");
strSql.Append("@DocId,@Content)");
Byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
fs.Dispose();
SqlParameter parameters1 = new SqlParameter("@Content", SqlDbType.Image, b.Length);
parameters1.Value = b;
SqlParameter parameters2 = new SqlParameter("@DocId", SqlDbType.VarChar,10);
parameters2.Value = "1001";
SqlCommand cmd = new SqlCommand(strSql.ToString(),con);
cmd.Parameters.Add(parameters1);
cmd.Parameters.Add(parameters2);
cmd.ExecuteNonQuery();

}
}

wuyq11 2009-04-10
  • 打赏
  • 举报
回复
不能操作关闭文件
路径是否能操作,权限可够。
Micao_tong 2009-04-10
  • 打赏
  • 举报
回复
本機和服務的路徑是不一樣的吧。
看看路徑是否一至。。。
dl_wang 2009-04-10
  • 打赏
  • 举报
回复
帮你顶一下
aspxbbs123 2009-04-10
  • 打赏
  • 举报
回复
.cs代码如下:
SqlConnection con = new SqlConnection("server=200.0.0.1;database=Temp;uid=sa;pwd=sa");
con.Open();
HttpFileCollection HFC = HttpContext.Current.Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile UserHPF = HFC[i];

if (UserHPF.ContentLength > 0)
{
System.IO.Stream fs = UserHPF.InputStream;
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into FileSaveTemp(");
strSql.Append("DocId,Content)");
strSql.Append(" values (");
strSql.Append("@DocId,@Content)");
Byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
SqlParameter parameters1 = new SqlParameter("@Content", SqlDbType.Image, b.Length);
parameters1.Value = b;
SqlParameter parameters2 = new SqlParameter("@DocId", SqlDbType.VarChar,10);
parameters2.Value = "1001";
SqlCommand cmd = new SqlCommand(strSql.ToString(),con);
cmd.Parameters.Add(parameters1);
cmd.Parameters.Add(parameters2);
cmd.ExecuteNonQuery();

}
}

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧