用FileStream上传图片转换成二进制,在本地用行,传到服务器上去出现如下错误

用FileStream上传图片转换成二进制,在本地用行,传到服务器上去出现如下错误
2009年07月04日 星期六 10:54

未能找到路径“C:\Documents and Settings\92724.jpg”的一部分。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.IO.DirectoryNotFoundException: 未能找到路径“C:\Documents and Settings\92724.jpg”的一部分。

怎么调..本地权限应该没问题,因为我在本地测没问题,而服务器权限应该还没到那个方面,因为它提示找不到本地路径...

所以我觉得是路径的问题,但是我不知道怎么改.代码如下..

C# code

string photoName1 = fileUp.PostedFile.FileName; //�获取初始文件名 FileStream fs = new FileStream(photoName1, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader br = new BinaryReader(fs); byte[] photo = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); OleDbCommand cmd = new OleDbCommand("update people set photo=@photo,[update]='" + this.time1.Text + "',realname='" + this.name1.Text + "',title='" + this.title1.Text + "',intro='" + this.con1.Text + "',article='" + this.come1.Text + "' where id=" + Request["id"], mycon_A); cmd.Parameters.Add("@photo", OleDbType.Binary, photo.Length).Value = photo;//把图片的二进制加到DB中 cmd.ExecuteNonQuery();

解决方法:你程序是运行在服务器的,你的文件是选择在客户端的,当然报图片不存在。。 

Stream fs = fileUp.PostedFil.InputStream;

BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();

OleDbCommand cmd = new OleDbCommand("update people set photo=@photo,[update]='" + this.time1.Text + "',realname='" + this.name1.Text + "',title='" + this.title1.Text + "',intro='" + this.con1.Text + "',article='" + this.come1.Text + "' where id=" + Request["id"], mycon_A);

cmd.Parameters.Add("@photo", OleDbType.Binary, photo.Length).Value = photo;//把图片的二进制加到DB中
cmd.ExecuteNonQuery();
总结:Stream 允许远程数据流上传,本地到服务器,本地到本地都没有问题;FileStream 只支持本地数据流上传,所有会造成本地上传没有问题,到服务器时错误

原文地址:https://www.cnblogs.com/andyyu/p/1652627.html