删除留言时怎么删除上传到文件夹里的图片?

shishikunkun 2011-01-24 10:35:17
我现在如果删除一条留言时,怎么删除上传的文件夹里的图片呢?我现在查询出来留言中包含图片了,
就不知道怎么删除图片:

public int Check_ContentImg(string post_id)
{
//同样如果要查询发的帖子里是否有音乐,媒体文件等只要在like中指定关键字
strsql = "select count(*) from Bbs_Post where Post_Content like '%.jpg%' or Post_Content like '%.gif' and Post_Id=" + post_id;
return ExecuteScalar(strsql);
}

public void Del_BbsPost(string postid)
{
if (bbs_post.Check_ContentImg(postid) > 0) //说明内容是有图片的
{
System.IO.File.Delete(文件路径)//删除文件夹中图片, 不知道如何得到路径 }
if (bbs_post.Del_BbsPost(postid)) //删除帖子方法
{
Jscript.Alert("删除帖子成功")
}
}

麻烦大家帮忙看看,或者是不是我的想法有错误,谢谢了
...全文
416 27 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
無葉 2011-01-24
  • 打赏
  • 举报
回复
内容中取得图片,

如果图片存在就删除,
shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
麻烦大家了,先结账吧。。可就是楼上的兄弟说的问题
Adechen 2011-01-24
  • 打赏
  • 举报
回复
看看你留言的时候是怎么个保存图片的?问题就迎刃而解了
shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 pcb_ghl 的回复:]
如果删除不了,
string temPath = HttpContext.Current.Server.MapPath( arrayList[i].ToString());
在这句代码加个断点,看下temPath的值.再看看对应的文件夹里有没有文件.测试一下.
[/Quote]
可以加了QQ么。。。不好意思,太麻烦你了。。
shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 pcb_ghl 的回复:]
Server.MapPath
获取的是与虚拟路径相对应的物理文件路径.
就是用这个文件路径来进行删除操作.
[/Quote]
: “http://localhost:3551/upfile/users/2011124114557923.gif”不是有效的虚拟路径。
应要把这里也去掉的,http://localhost:3551,要这样删("upfile/users/2011124114557923.gif")
才对吧,我试了楼上的很多方法,都是这个问题。。
pcb_ghl 2011-01-24
  • 打赏
  • 举报
回复
如果删除不了,
string temPath = HttpContext.Current.Server.MapPath( arrayList[i].ToString());
在这句代码加个断点,看下temPath的值.再看看对应的文件夹里有没有文件.测试一下.
pcb_ghl 2011-01-24
  • 打赏
  • 举报
回复
Server.MapPath
获取的是与虚拟路径相对应的物理文件路径.
就是用这个文件路径来进行删除操作.
shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 pcb_ghl 的回复:]
引用 14 楼 shishikunkun 的回复:
引用 11 楼 pcb_ghl 的回复:
写个正则来匹配内容中的图片吧.

C# code

using System.Text.RegularExpressions;
private static Regex RegEditImg = new Regex(@"img[^>]*src\s*=\s*[""|']?(?<url>[^""……
[/Quote]
兄弟还在波?你的代码还有点小问题
string temPath = HttpContext.Current.Server.MapPath( arrayList[i].ToString());
//这里取的貌似是绝对路径,我要取相对路径,怎么改。。
孟子E章 2011-01-24
  • 打赏
  • 举报
回复
简单有效的方法,在你发布留言的时候,记住上传的文件路径。和留言ID关联
huangwenquan123 2011-01-24
  • 打赏
  • 举报
回复

string str = @"<img width=100 src=""files.jpg""> This is a test page <img width=100 src=""files2.jpg""> hello world, html test <img width=100 src=""ffword.jpg""> from here, wodd. <span></span>
This is a test page <img width=100 src=""files2.jpg""> hello world, html test <img width=100 src=""ffword.jpg""> from here, wodd. <span></span>";
Regex reg = new Regex(@"(?is)<img[^>]*?src=(['""\s])?(?<src>[^'""\s]*?)\1[^>*]?>");
MatchCollection match = reg.Matches(str);
foreach (Match m in match)
{
Response.Write(m.Groups["src"].Value);
}
//删除的时候先根据id获取留言内容,在用正择匹配出图片路径
//然后在File.Delete(路径);
//最后在把留言内容删了
pcb_ghl 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 shishikunkun 的回复:]
引用 11 楼 pcb_ghl 的回复:
写个正则来匹配内容中的图片吧.

C# code

using System.Text.RegularExpressions;
private static Regex RegEditImg = new Regex(@"img[^>]*src\s*=\s*[""|']?(?<url>[^""']*)[""|']?[^<>]*>", RegexO……
[/Quote]

这个还怎么说啊?
这段代码的意思就是你通过数据查询出来的文章内容,正则匹配图片,将图片地址放入一个ArrayList,并返回这个 ArrayList.

using System.Text.RegularExpressions;
private static Regex RegEditImg = new Regex(@"img[^>]*src\s*=\s*[""|']?(?<url>[^""']*)[""|']?[^<>]*>", RegexOptions.IgnoreCase);
public static ArrayList ImgUrl(string inputData)
{
MatchCollection matchs = RegEditImg.Matches(inputData);
ArrayList Img = new ArrayList();
foreach(Match m in matchs)
{
Img.Add(m.Groups[1].Value);
}
return Img;
}



下面这段代码放在一个Button的click事件中

......根据文章ID查询文章内容代码省略.
sqlRead.Read();
ArrayList arrayList = ImgUrl(sqlRead["Article"].ToString()); //调用上面的正则的方法,将获取的图片地址放到一个ArrayList中
for (int i = 0; i < arrayList.Count; i++)
{
string temPath = Server.MapPath(arrayList[i].ToString());
if (File.Exists(temPath))
{
File.Delete(temPath);
}
}




shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 pcb_ghl 的回复:]
写个正则来匹配内容中的图片吧.

C# code

using System.Text.RegularExpressions;
private static Regex RegEditImg = new Regex(@"img[^>]*src\s*=\s*[""|']?(?<url>[^""']*)[""|']?[^<>]*>", RegexOptions.Ig……
[/Quote]
我看了下,虽然看的不是很懂,但是我想,这样会不会识删除呢,我是想由留言id找到留言内容中的图片路径(或者路径名),然后再删除,这样删除我认为是最保险的
wuyq11 2011-01-24
  • 打赏
  • 举报
回复
正则获取图片文件,遍历删除string fckStr = FCKeditor1.Value;
MatchCollection matchs = Regex.Matches(fckStr, @ " <img\s[^> ]*src=([ " " ']*)(? <src> [^ ' " "]*)\1[^> ]*> ", RegexOptions.IgnoreCase);
foreach (Match m in matchs) {
Response.Write(m.Groups[ "src "].Value+ " <br> ");
}
Mandys 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxr0323 的回复:]
System.IO.File.Delete(文件路径)

这个图片路径怎么获取呢?

你的文件夹在哪了 可以先指定一个地址吧

你的图片路径是存在数据库吗?

那就根据帖子的ID 去数据库中寻找图片的路径。
[/Quote]

+1
pcb_ghl 2011-01-24
  • 打赏
  • 举报
回复
写个正则来匹配内容中的图片吧.

using System.Text.RegularExpressions;
private static Regex RegEditImg = new Regex(@"img[^>]*src\s*=\s*[""|']?(?<url>[^""']*)[""|']?[^<>]*>", RegexOptions.IgnoreCase);
public static ArrayList ImgUrl(string inputData)
{
MatchCollection matchs = RegEditImg.Matches(inputData);
ArrayList Img = new ArrayList();
foreach(Match m in matchs)
{
Img.Add(m.Groups[1].Value);
}
return Img;
}

然后

ArrayList arrayList = ManageNews.ImgUrl(sqlRead["Article"].ToString()); //将获取的图片地址放到一个ArrayList中
for (int i = 0; i < arrayList.Count; i++)
{
string temPath = Server.MapPath(arrayList[i].ToString());
if (File.Exists(temPath))
{
File.Delete(temPath);
}
}

没测试,应该差不多.
flyerwing 2011-01-24
  • 打赏
  • 举报
回复
删除就删除,怎么还来个怎么删除.
shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chen_ya_ping 的回复:]
引用 7 楼 shishikunkun 的回复:

引用 4 楼 phil999 的回复:
不知道如何得到路径

你自己都不知道,别人就更不知道了。一般是两种办法,如果前面设计的好,凡是上传的(而不是引用网上的)的图片都在数据库里加一条记录,那么删起来很简单,不必分析post内容。如果没有对图片进行过跟踪,那么可以用正则表达式分析post内容,把图片路径取出来,不过效果就不那么好了。
……
[/Quote]
怎么删除?图片只是留言内容的一部分,现在主要问题是如果才能从留言内容中得到图片的路径(可能是多张),我的想法,是可以由图片后缀名来删除么?
chen_ya_ping 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shishikunkun 的回复:]

引用 4 楼 phil999 的回复:
不知道如何得到路径

你自己都不知道,别人就更不知道了。一般是两种办法,如果前面设计的好,凡是上传的(而不是引用网上的)的图片都在数据库里加一条记录,那么删起来很简单,不必分析post内容。如果没有对图片进行过跟踪,那么可以用正则表达式分析post内容,把图片路径取出来,不过效果就不那么好了。

我也知道,还有我的上传图片是我重新命名过由这样的方……
[/Quote]
就按照数据库中记录的名称来删除。
shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 phil999 的回复:]
不知道如何得到路径

你自己都不知道,别人就更不知道了。一般是两种办法,如果前面设计的好,凡是上传的(而不是引用网上的)的图片都在数据库里加一条记录,那么删起来很简单,不必分析post内容。如果没有对图片进行过跟踪,那么可以用正则表达式分析post内容,把图片路径取出来,不过效果就不那么好了。
[/Quote]
我也知道,还有我的上传图片是我重新命名过由这样的方式来命名:datatime.now.tostring("yy-MM-dd MM:hh:ss")
shishikunkun 2011-01-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jxyxhz 的回复:]
bbs_post.Check_ContentImg(postid)

这个方法应该可以获取图片的相对路径的。
[/Quote]
你的意思,当这个模糊查询时,会连文件名一起查询出来么?我还真没有注意,我试试看
加载更多回复(7)

62,248

社区成员

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

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

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

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