.net上传图片的问题

zhujuanfang 2012-05-15 09:57:28
各位高手,我现在碰到了一个奇怪的问题。就是我的一个网站源文件在我自己电脑上运行,用FileUpload上传图片都没问题,但是放到我们的服务器上,就报错。就说什么路径有问题。我明明路径弄的都是一样的。不知道是不是我服务器上漏了什么文件,导致那个上传图片控件出错,请大家帮我看看?实在是没办法了
...全文
95 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoruogao 2012-05-17
  • 打赏
  • 举报
回复
打印一下 捕获异常 ex 的错误消息,看看
孟子E章 2012-05-15
  • 打赏
  • 举报
回复
如果路径有权限并且存在,调用正确的话,是应该可以的
孟子E章 2012-05-15
  • 打赏
  • 举报
回复
public string UploadPicFile(FileUpload FileUp)
{
string filefullname = FileUp.FileName;
string filename = System.IO.Path.GetFileName(filefullname);
string type = filename.Substring(filename.LastIndexOf(".") + 1);
type = type.ToLower();

string imgurl = "";
if (type == "jpg" || type == "gif" || type == "png")
{
try
{
if (FileUp.HasFile)
{
if ((FileUp.PostedFile.ContentLength) / 1024 > 3072)
{
Page.ClientScript.RegisterStartupScript(GetType(), "x", "alert('上传图片大小不能超过3M');", true);
return string.Empty;
}
else
{
FileUp.PostedFile.SaveAs(Server.MapPath("~/webadmin/product/images") + "\\" + filename);
imgurl = Page.ResolveUrl("~") + "webadmin/product/images/" + filename;
return imgurl;
}
}
else
{
return string.Empty;
}

}
catch (Exception ex)
{
//throw ex;
Page.ClientScript.RegisterStartupScript(GetType(), "xxx", "alert('上传图片错误,请检查路径!');", true);
return string.Empty;
}
}
else
{
//Lable_msg.Text = "图片格式应为bmp,jpg,gif";
return string.Empty;
}
}
cf_nxcxy 2012-05-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
求真相 贴图 贴码
[/Quote]
en
zhujuanfang 2012-05-15
  • 打赏
  • 举报
回复
public string UploadPicFile(FileUpload FileUp)
{
string filefullname = FileUp.PostedFile.FileName;
string filename = filefullname.Substring(filefullname.LastIndexOf("\\") + 1);
string type = filename.Substring(filename.LastIndexOf(".") + 1);
string imgurl = "";
if ( type == "jpg" || type == "gif" || type == "JPG" || type == "png" || type == "GIF"|| type == "PNG")
{
try
{
if (FileUp.HasFile)
{
if ((FileUp.PostedFile.ContentLength) / 1024 > 3072)
{
ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), "alert('上传图片大小不能超过3M');", true);
return string.Empty;
}
else
{
FileUp.PostedFile.SaveAs(this.Server.MapPath("~/webadmin/product/images") + "\\" + filename);
imgurl = "~/webadmin/product/images" + "/" + filename;
return imgurl;
}
}
else
{
return string.Empty;
}

}
catch(Exception ex)
{
//throw ex;
ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), "alert('上传图片错误,请检查路径!');", true);
return string.Empty;
}
}
else
{
//Lable_msg.Text = "图片格式应为bmp,jpg,gif";
return string.Empty;
}
}

这个就是我写的上传图片的方法
  • 打赏
  • 举报
回复
求真相 贴图 贴码
seekboya 2012-05-15
  • 打赏
  • 举报
回复
if (this.FileUpload.FileName == "")
{
Response.Write("<script>alert('请选择上传文件!');history.go(-1); </script>");
return;
}
if (this.FileUpload.PostedFile.ContentType.ToLower().IndexOf("image") >= 0)
{
string info_pic = System.Guid.NewGuid().ToString("D") + System.IO.Path.GetExtension(this.FileUpload.FileName);
this.FileUpload.SaveAs(Server.MapPath("../../upfile/" + info_pic));
if (db.db_banner_ADD(webguid, int.Parse(_target), _url, int.Parse(_px), info_pic) == 1)
{
Response.Write("<script>alert('添加成功!');window.location.href='compic.aspx';</script>");
return;
}
else
{
if (System.IO.File.Exists(Server.MapPath("../../upfile/" + info_pic)))
{
System.IO.File.Delete(Server.MapPath("../../upfile/" + info_pic));
}

Response.Write("<script>alert('数据写入失败,请重试!');window.location.href='compic.aspx';</script>");
return;
}
}
else
{
Response.Write("<script>alert('文件类型必须为图片类型!');history.go(-1); </script>");
return;
}

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧