我下面这段上传图片的代码老是报错,希望各位高手都来帮我看看!!!
private void btnPostUpImage_Click(object sender, System.EventArgse)
{
string path;
string filename;
int po;
long filesize;
string fType;
if(file1.PostedFile.FileName.Trim()!="")
{
try
{
path=Server.MapPath("..//UpFile").ToString();
filename=file1.PostedFile.FileName.ToString(); fType=file1.PostedFile.ContentType.ToString();
this.ViewState["type"]=fType;
filesize=file1.PostedFile.ContentLength;
po=filename.LastIndexOf((char)92);
filename=filename.Substring(po+1,filename.Length-po-1);
this.ViewState["PostFileName"]=filename;
file1.PostedFile.SaveAs(path+"//"+filename);
this.imageCine.ImageUrl="..//UpFile//"+filename;
}
catch(Exception ex)
{
this.ShowErrMsg(ex.Message);
}
}
}错误信息是:
对路径C:InetpubwwwrootWebCinemaServerUpFile的访问被拒绝
private void btnPostUpImage_Click(object sender, System.EventArgse)
{
string path;
string filename;
int po;
long filesize;
string fType;
if(file1.PostedFile.FileName.Trim()!="")
{
try
{
path=Server.MapPath("..//UpFile").ToString();
filename=file1.PostedFile.FileName.ToString(); fType=file1.PostedFile.ContentType.ToString();
this.ViewState["type"]=fType;
filesize=file1.PostedFile.ContentLength;
po=filename.LastIndexOf((char)92);
filename=filename.Substring(po+1,filename.Length-po-1);
this.ViewState["PostFileName"]=filename;
file1.PostedFile.SaveAs(path+"//"+filename);
this.imageCine.ImageUrl="..//UpFile//"+filename;
}
catch(Exception ex)
{
this.ShowErrMsg(ex.Message);
}
}
}错误信息是:
对路径C:InetpubwwwrootWebCinemaServerUpFile的访问被拒绝
C:InetpubwwwrootWebCinemaServerUpFile 你这个路径咋没有“\” ?
你看,下面的代码就有N个多余的 ToStringpath=Server.MapPath("..//UpFile").ToString();//MapPath返回的就是string
filename=file1.PostedFile.FileName.ToString();//FileName属性就是string fType=file1.PostedFile.ContentType.ToString();//上同还有就是浪费内存
filesize=file1.PostedFile.ContentLength;//变量定义的是long,但根本用不到
//ContentLength属性只是intthis.imageCine.ImageUrl="..//UpFile//"+filename;// 你这里的“//”是想转义吗?!但'/'根本不用转义,而且转义也不这样转的,应该是\