多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 方法调用 ``` string dtpath = DateTime.Now.ToString("yyyyMMdd"); string dtname = DateTime.Now.ToString("yyyyMMddHHmmssfff_" + Operation.Number(8)); string filepath = context.Server.MapPath("~\\upload\\image\\" + dtpath + ""); filepath = (filepath.IndexOf("~") > -1) ? context.Server.MapPath(filepath) : filepath; HttpFileCollection files = context.Request.Files; if (!Directory.Exists(filepath))//如果文件夹不存在创建文件夹 Directory.CreateDirectory(filepath); string filepaths = context.Server.MapPath("~\\upload\\image\\" + dtpath + "\\" + dtname + ".jpg"); string namepath = "/upload/image/" + dtpath + "/" + dtname + ".jpg"; bool li = SrcLoadImg.SavePhotoFromUrl(filepaths, imgsrcArr[i]); ``` # 代码 ``` using System; using System.IO; using System.Net; namespace NetWing.Common { public static class SrcLoadImg { /// <summary> /// 从Url保存图片到本地 /// </summary> /// <param name="FileName">文件名</param> /// <param name="Url">远程地址</param> /// <returns></returns> public static bool SavePhotoFromUrl(string FileName, string Url) { bool value = false; WebResponse response = null; Stream stream = null; try { WebRequest request = WebRequest.Create(Url); response = request.GetResponse(); stream = response.GetResponseStream(); if (!response.ContentType.ToLower().StartsWith("text/")) { value = SaveBinaryFile(response, FileName); } } catch (Exception e) { string aa = e.ToString(); CWriteLogs.WriteLogsE("Logs", "Error >> SavePhotoFromUrl", e.Message); } return value; } private static bool SaveBinaryFile(WebResponse response, string FileName) { bool Value = true; byte[] buffer = new byte[1024]; try { if (!File.Exists(FileName)) { Stream outStream = System.IO.File.Create(FileName); Stream inStream = response.GetResponseStream(); int l; do { l = inStream.Read(buffer, 0, buffer.Length); if (l > 0) outStream.Write(buffer, 0, l); } while (l > 0); outStream.Dispose(); outStream.Close(); inStream.Close(); } } catch (Exception e) { Value = false; CWriteLogs.WriteLogsE("Logs", "Error >> SaveBinaryFile", e.Message); } return Value; } } } ```