public ActionResult uploaderPhotoByUrl(int customerId, string url, int groupId, string callback) { string resultJson = ""; string json = ""; WebRequest request = null; WebResponse response = null; Stream reader = null; FileStream writer = null; Image image = null; try { request = WebRequest.Create(url); response = request.GetResponse(); reader = response.GetResponseStream(); string contentType = response.ContentType; if (contentType.Equals("image/jpeg") || contentType.Equals("image/gif") || contentType.Equals("image/webp") || contentType.Equals("image/png") || contentType.Equals("image/jpg")) { string extName = contentType.Split('/')[1]; string name = ""; if (extName.Equals("webp")) { name = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(10000, 99999) + ".jpg"; } else { name = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(10000, 99999) + "." + extName; } string imageFileName = System.DateTime.Now.ToString("yyyyMMdd") + "/" + name; string path = string.Format("/resource/images/photo/{0}/", customerId) + imageFileName; string servicePath = Server.MapPath(path); FileUploadHelper.CreateDirectory(servicePath); writer = new FileStream(servicePath, FileMode.OpenOrCreate, FileAccess.Write); byte[] buff = new byte[512]; int c = 0; //实际读取的字节数 while ((c = reader.Read(buff, 0, buff.Length)) > 0) { writer.Write(buff, 0, c); } writer.Close(); writer.Dispose(); string base64Image = Convert.ToBase64String(buff); image = Image.FromFile(servicePath); string size = image.Width + "x" + image.Height; image.Dispose(); string smallName = string.Format("/resource/images/photo/{0}/small/", customerId) + imageFileName; string thumbName = string.Format("/resource/images/photo/{0}/thumb/", customerId) + imageFileName; FileUploadHelper.UploadSmallImage(path, smallName, ThumbCompress.小图); FileUploadHelper.UploadSmallImage(path, thumbName, ThumbCompress.缩略图); int count = GalleryBLL.Instance.Add(new GalleryModel() { Callery_BigPic = path, Callery_Customer_ID = customerId, Callery_Name = name, Callery_Size = size, Callery_SmallPic = smallName, Callery_ThumbnailPic = thumbName, Callery_Time = DateTime.Now, Callery_UpdateTime = DateTime.Now, Photo_FatherID = groupId }); if (count > 0) { json = JsonConvert.SerializeObject(new ApiResult(HQEnums.ResultOptionType.OK, path)); } else { json = JsonConvert.SerializeObject(new ApiResult(HQEnums.ResultOptionType.图片失败)); } } else { json = JsonConvert.SerializeObject(new ApiResult(HQEnums.ResultOptionType.缺少请求参数, "图片格式错误,请提取jpeg,gif,png,webp,jpg格式的图片")); } } catch (Exception ex) { LogHelper.WriteError(string.Format("GetPhotoByUrl error-->Statck:{0},Message:{1}", ex.StackTrace, ex.Message)); json = JsonConvert.SerializeObject(new ApiResult(HQEnums.ResultOptionType.务器错误)); } finally { if (reader != null) { reader.Close(); reader.Dispose(); } if (response != null) { response.Close(); } } if (!string.IsNullOrEmpty(callback)) { resultJson = callback + "(" + json + ")"; } else { resultJson = json; } return(Content(resultJson, "application/json")); }