我写的一段上传文件的代码,上传文件时出错,提示找不到文件,,,当本地的文件路径没有错,而且只有当服务器端同样的路径下有同名文件存在时上传开成功,,不知道问题出在哪里??请大伙帮我看看。代码如下:
protected System.Web.UI.WebControls.TextBox txttAccessoryID;
protected System.Web.UI.WebControls.TextBox txtAccessoryID;
protected System.Web.UI.WebControls.TextBox txtAccessory;
protected System.Web.UI.HtmlControls.HtmlInputFile upaccessory;private void btnup_Click(object sender, System.EventArgs e)
{
if(upaccessory.PostedFile.FileName != "")
{
string convert;
string strFullPath=upaccessory.PostedFile.FileName;
string filename=strFullPath.Substring(strFullPath.LastIndexOf("\\") + 1);
string extent=filename.Substring(filename.LastIndexOf(".")+1); txtAccessory.Text+=filename;
convert = DateTime.Now.ToString()+"."+extent;
convert = convert.Replace(":","-");
txtAccessoryID.Text += convert;
filename=txtAccessoryID.Text; string path=Server.MapPath("file");
upaccessory.PostedFile.SaveAs(path+"\\"+filename);
}
}
protected System.Web.UI.WebControls.TextBox txttAccessoryID;
protected System.Web.UI.WebControls.TextBox txtAccessoryID;
protected System.Web.UI.WebControls.TextBox txtAccessory;
protected System.Web.UI.HtmlControls.HtmlInputFile upaccessory;private void btnup_Click(object sender, System.EventArgs e)
{
if(upaccessory.PostedFile.FileName != "")
{
string convert;
string strFullPath=upaccessory.PostedFile.FileName;
string filename=strFullPath.Substring(strFullPath.LastIndexOf("\\") + 1);
string extent=filename.Substring(filename.LastIndexOf(".")+1); txtAccessory.Text+=filename;
convert = DateTime.Now.ToString()+"."+extent;
convert = convert.Replace(":","-");
txtAccessoryID.Text += convert;
filename=txtAccessoryID.Text; string path=Server.MapPath("file");
upaccessory.PostedFile.SaveAs(path+"\\"+filename);
}
}
upaccessory.PostedFile.SaveAs(path+"\\"+filename);
Server.MapPath()取当前文件的绝对目录 file 文件要存在。这样试试 string path=Server.MapPath(".\\file\\");
save才可以.要绝对路径
现在的问题是,比如说我瑶上传客户端C:\TEST.TXT文件到服务器上,提示错误,如果服务器上同样存在C:\TEST.TXT文件,则成功。。而上传的文件实际上市客户端的TEST.TXT文件,,,不知道为什么??