1个回答
2013-08-12
展开全部
/// <summary>
/// 下载远程图片
/// </summary>
/// <param name="strUrl">将要下载的图片地址</param>
/// <returns>返回string类型;图片的本地地址</returns>
public static string GetRemoteImage(string strUrl)
{
try
{
string fileExt = System.IO.Path.GetExtension(strUrl);
Random ro=new Random((int)DateTime.Now.Ticks);
Random ro2 = new Random(Guid.NewGuid().GetHashCode());
string PathStr =HttpContext.Current.Server.MapPath("/");
string filePath = @"\download\"+ DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MMdd") + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ro.Next(1000) + fileExt; string tempFolder = System.IO.Path.GetDirectoryName(PathStr + filePath);
if( !System.IO.Directory.Exists( tempFolder ) )
{
System.IO.Directory.CreateDirectory(tempFolder);
} WebClient wc = new WebClient();
wc.DownloadFile(strUrl,PathStr + filePath);
wc.Dispose();
return filePath;
}
catch
{
//System.Web.HttpContext.Current.Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('获取远程图片出错!原因如下:\\n"+ err.Message +"');</script>\n");
//HttpContext.Current.Response.Write(Functions.ShowErr("获取远程图片出错!原因如下:\\n" + ,1));
//HttpContext.Current.Response.End();
return "";
}
}
/// 下载远程图片
/// </summary>
/// <param name="strUrl">将要下载的图片地址</param>
/// <returns>返回string类型;图片的本地地址</returns>
public static string GetRemoteImage(string strUrl)
{
try
{
string fileExt = System.IO.Path.GetExtension(strUrl);
Random ro=new Random((int)DateTime.Now.Ticks);
Random ro2 = new Random(Guid.NewGuid().GetHashCode());
string PathStr =HttpContext.Current.Server.MapPath("/");
string filePath = @"\download\"+ DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MMdd") + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ro.Next(1000) + fileExt; string tempFolder = System.IO.Path.GetDirectoryName(PathStr + filePath);
if( !System.IO.Directory.Exists( tempFolder ) )
{
System.IO.Directory.CreateDirectory(tempFolder);
} WebClient wc = new WebClient();
wc.DownloadFile(strUrl,PathStr + filePath);
wc.Dispose();
return filePath;
}
catch
{
//System.Web.HttpContext.Current.Response.Write("<script language='JavaScript' type='text/JavaScript'>alert('获取远程图片出错!原因如下:\\n"+ err.Message +"');</script>\n");
//HttpContext.Current.Response.Write(Functions.ShowErr("获取远程图片出错!原因如下:\\n" + ,1));
//HttpContext.Current.Response.End();
return "";
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询