string sPath = Server.MapPath(../upload/ path);这句代码在挑选图片上传的时候需注意什么?

[复制链接]
查看11 | 回复4 | 2011-5-21 11:48:44 | 显示全部楼层 |阅读模式
最近在研究图片上传并且保存到数据库中(access数据库)然后再上传中遇到问题了
stringpath=FileUpload1.PostedFile.FileName.ToString();
stringsPath=Server.MapPath(\"../upload/\"path);这句代码是不是意味着在挑选图片上传的时候要挑选upload文件夹下的图片?我试了一下上传在F:\\网络相册\\upload\\13-05-11_10.26[01].jpg的这张图,但是运行到这句代码的时候他说:“不是有效的虚拟路径”
那请问问题出在哪里了?
我的上传按钮的事件代码如下:
protectedvoidbtnadd_Click(objectsender,EventArgse)
{
//stringid=newRandom().Next(1,1000).ToString();
//将附件传送到服务器上
stringpath=FileUpload1.PostedFile.FileName.ToString();//获取上传图片路径
stringext=path.Substring(path.LastIndexOf(\".\")1);//获取图片扩展名
if(ext.ToLower()==\"jpg\"||ext.ToLower()==\"bmp\"||ext.ToLower()==\"gif\"||ext.ToLower()==\"png\"||ext.ToLower()==\"tif\"||ext.ToLower()==\"jpeg\")
{
stringphototitle=this.txttitle.Text.ToString();
stringphotocontent=this.txtcontent.Text.ToString();
stringwPath=\"../upload/\"path;
stringsPath=Server.MapPath(\"../upload/\"path);//设置图片保存到服务器上路径
stringsuoPath=Server.MapPath(\"../suo/\")path;
//path=\"..\\\\Photo\\\\\"PhotoID\".\"ext;//保存到数据库中路径
FileUpload1.PostedFile.SaveAs(sPath);//保存到服务器
stringurlPath=wPath;
stringphotoUser=\"admin\";
inttype=Convert.ToInt32(this.dropdownlists.SelectedValue.ToString());
intcount=newphoto().addpic(phototitle,type,path,photocontent,photoUser);
if(count0)
{
PictureSlightly.MakeImage(sPath,suoPath,100,100,\"hw\");
this.Page.ClientScript.RegisterStartupScript(this.GetType(),null,\"scriptalert(\'添加成功!\');/script\");
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(),null,\"scriptalert(\'添加失败!\');/script\");
}
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(),null,\"scriptalert(\'只能上传图片!\');/script\");
}

}
最好能将每句代码的意思都说一下哈,谢谢啦~
回复

使用道具 举报

千问 | 2011-5-21 11:48:44 | 显示全部楼层
//stringid=newRandom().Next(1,1000).ToString();
id赋值为1-1000内的随机数
stringpath=FileUpload1.PostedFile.FileName.ToString();
获取上传图片路径
stringext=path.Substring(path.LastIndexOf(\".\")1);
获取图片扩展名
if(ext.ToLower()==\"jpg\"||ext.ToLower()==\"bmp\"||ext.ToLower()==\"gif\"||ext.ToLower()==\"png\"||ext.ToLower()==\"tif\"||ext.ToLower()==\"jpeg\")
{
当扩展名满足条件进入
stringphototitle=this.txttitle.Text.ToString();
获取图片标题
stringphotocontent=this.txtcontent.Text.ToString();
获取图片说明
stringwPath=\"../upload/\"path;
图片路径(../upload)是服务器上的默认图片路径
stringsuoPath=Server.MapPath(\"../suo/\")path;
stringsPath=Server.MapPath(\"../upload/\"path);
设置图片保存到服务器上路径
FileUpload1.PostedFile.SaveAs(sPath);
保存到服务器

stringurlPath=wPath;
stringphotoUser=\"admin\";
inttype=Convert.ToInt32(this.dropdownlists.SelectedValue.ToString());
一些属性的赋值
intcount=newphoto().addpic(phototitle,type,path,photocontent,photoUser);
应该是他自定义好的数据库插入方法
if(count0)
{
PictureSlightly.MakeImage(sPath,suoPath,100,100,\"hw\");
this.Page.ClientScript.RegisterStartupScript(this.GetType(),null,\"scriptalert(\'添加成功!\');/script\");
}else{
this.Page.ClientScript.RegisterStartupScript(this.GetType(),null,\"scriptalert(\'添加失败!\');/script\");
}
添加以后通过受影响行数判断是否成功
}else
{
不满足图片扩展名
this.Page.ClientScript.RegisterStartupScript(this.GetType(),null,\"scriptalert(\'只能上传图片!\');/script\");
}
--------------------------------------------华丽的分割线-------------------------------------------
注释写的算很清楚如果是web项目的图片上传还有其他的方法
一般我感觉用输入输出流的比较多也有在数据库里存2进制数据的









<h4class=\"ask\">追问


那我在选择图片上传的时候他说:
../upload/C:\\Users\\zhuzhu\\Desktop\\P13-05-11_10.26[01].jpg不是有效的虚拟路径
使得我的图片不能上传成功
那该怎么办呀?
我不是很懂埃麻烦了。
回复

使用道具 举报

千问 | 2011-5-21 11:48:44 | 显示全部楼层
../upload/这个应该是服务器上的相对路径

然后你又有一个C:\\的物理磁盘的绝对路径肯定不行啊
回复

使用道具 举报

千问 | 2011-5-21 11:48:44 | 显示全部楼层
谢谢哈。
那我在这些代码的基础上该怎么选择上传的图片呢?
回复

使用道具 举报

千问 | 2011-5-21 11:48:44 | 显示全部楼层
看情况是一个页面上传图片

至少要在页面的inputtype=\'file\'/有一个type的值等于file
在上传的表单上设置为提交数据为文件类型
因为你上面的代码不太完整有一个FileUpload1内的具体代码不多
所以选择图片是通过页面的input标签内的type=file就可以直觉选择图片路径不会有错
../upload应该是服务器上的路径你调整一下图片上传后的储存路径就差不多
-------------------------------------------------------------
stringsPath=Server.MapPath(\"../upload/\"path);
-------------------------------------------------------------
应该是这个的path路径不对应该直接是文件名了P13-05-11_10.26[01].jpg
也就是说你将本地的P13-05-11_10.26[01].jpg这个图片保存到服务器上的upload文件夹
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行