我在本地上传一张图片到服务器上,,总是报错说找不到我要上传的文件,后来经过测试发现是他把我本地的图片路径读到了服务器上,在服务器上当让是找不到这张图片的,请高手指点下要怎么才能把本地图片上传到服务器
string imgUrl = ConfigurationManager.AppSettings["ImageUrl"];
string physicalPath = this.Page.MapPath(@imgUrl);//指定文件上传路径
// string physicalPath = this.Page.MapPath(@"..\..\UserImages");//指定文件上传路径
string fileName = fScheduleNm + fileType;
string path_fileName = physicalPath + fileName; if (File.Exists(path_fileName)) {
File.Delete(path_fileName);
}
this.fpdUpload.PostedFile.SaveAs(path_fileName);
string imgUrl = ConfigurationManager.AppSettings["ImageUrl"];
string physicalPath = this.Page.MapPath(@imgUrl);//指定文件上传路径
// string physicalPath = this.Page.MapPath(@"..\..\UserImages");//指定文件上传路径
string fileName = fScheduleNm + fileType;
string path_fileName = physicalPath + fileName; if (File.Exists(path_fileName)) {
File.Delete(path_fileName);
}
this.fpdUpload.PostedFile.SaveAs(path_fileName);
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
Label1.Text = "您还没有选择图片!";
return;
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
string filename=filepath.Substring(filepath.LastIndexOf("\\")+1);
string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1);
string serverpath = Server.MapPath("File/") + filename;
if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif")
{
FileUpload1.PostedFile.SaveAs(serverpath);
Image1.ImageUrl = "File/" + filename;
Label1.Text = "上传成功!";
}
else
{
Label1.Text = "上传的图片扩展名错误!";
}
}
}
string type = name.Substring(name.LastIndexOf(".") + 1); //获得文件的类型
FileStream fs = File.OpenRead(name); //以文件流方式读取该文件
byte[] content = new byte[fs.Length]; //获得该文件流的长度
fs.Read(content, 0, content.Length); //从流中读取文件
fs.Close(); string picname = tb_name.Text;
int typeid = bp.PhotoClassID(ddl_type.Text);
string re = tb_re.Text; if (type == "jpg" || type == "gif" || type == "png" || type == "bmp") //符合以下文件后缀名的文件
{
bpo.AddPhotos(picname, typeid, content, re);
Response.Redirect("ViewPicture.aspx");
}