/// <summary>
/// 上传微信头像到服务器
/// </summary>
/// <param name="imgUrl"></param>
/// <param name="fileName"></param>
/// <returns></returns>
private bool UploadImgToServer(string imgUrl, string fileName)
{
bool isSuccess = false;
try
{
//检查文件夹是否存在
string mapPath = HttpContext.Current.Server.MapPath("~/Upload/HeadImg/");
if (!Directory.Exists(mapPath))
{
Directory.CreateDirectory(mapPath);
}
fileName = fileName + ".jpg";
string savePath = mapPath + fileName;
//检查文件是否已经存在,存在则删除后重新创建,保证头像最新并且一个用户只有一张图片储存在服务器
if (File.Exists(fileName))
{
File.Delete(fileName);
}
int timeOut = -1;
WebResponse response = null;
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
if (timeOut != -1) request.Timeout = timeOut;
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
isSuccess = SaveBinaryFile(response, savePath);
}
finally
{
if (stream != null) stream.Close();
if (response != null) response.Close();
}
}
catch (Exception)
{
isSuccess = false;
}

return isSuccess;
}

private bool SaveBinaryFile(WebResponse response, string savePath)
{
bool value = false;
byte[] buffer = new byte[1024];
Stream outStream = null;
Stream inStream = null;
try
{
if (File.Exists(savePath)) File.Delete(savePath);
outStream = System.IO.File.Create(savePath);
inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0) outStream.Write(buffer, 0, l);
} while (l > 0);
value = true;
}
finally
{
if (outStream != null) outStream.Close();
if (inStream != null) inStream.Close();
}
return value;
}

C#图片保存到本地的更多相关文章

  1. php 获取远程图片保存到本地

    php 获取远程图片保存到本地 使用两个函数 1.获取远程文件 2.把图片保存到本地 /** * 获取远程图片并把它保存到本地 * $url 是远程图片的完整URL地址,不能为空. */ functi ...

  2. iOS 将图片保存到本地

    //将图片保存到本地 + (void)SaveImageToLocal:(UIImage*)image Keys:(NSString*)key {     NSUserDefaults* prefer ...

  3. iOS-iOS调用相机调用相册【将图片保存到本地相册】

    设置头部代理 <UINavigationControllerDelegate, UIImagePickerControllerDelegate> 1.调用相机 检测前置摄像头是否可用 - ...

  4. Android View转为图片保存为本地文件,异步监听回调操作结果;

    把手机上的一个View或ViewGroup转为Bitmap,再把Bitmap保存为.png格式的图片: 由于View转Bitmap.和Bitmap转图片都是耗时操作,(生成一个1M的图片大约500ms ...

  5. js截图及绕过服务器图片保存至本地(html2canvas)

    今天要分享的是用html2canvas根据自己的需求生成截图,并且修复html2canvas截图模糊,以及绕过服务器图片保存至本地. 只需要短短的几行代码,就能根据所需的dom截图,是不是很方便,但是 ...

  6. React Native之图片保存到本地相册(ios android)

    React Native之图片保存到本地相册(ios android) 一,需求分析 1,react native保存网络图片到相册,iOS端可以用RN自带的CameraRoll完美解决,但是andr ...

  7. FFmpeg解码视频帧为jpg图片保存到本地

    FFmpeg解码视频帧为jpg图片保存到本地 - CSDN博客 https://blog.csdn.net/qq_28284547/article/details/78151635

  8. java将base64解析图片保存到本地。

    将base64解析图片保存到本地的两个方法 /** * base64转图片 * @param base64str base64码 * @param savePath 图片路径 * @return */ ...

  9. 2018-5-22-SublimeText-粘贴图片保存到本地

    title author date CreateTime categories SublimeText 粘贴图片保存到本地 lindexi 2018-05-22 15:15:26 +0800 2018 ...

  10. 微信小程序图片保存到本地

    微信小程序图片保存到本地是一个常用功能: 这里讲解下完整实现思路: 因为微信官方的授权只弹一次,用户拒绝后再次调用,就需要结合button组件的微信开放能力来调起,以下方案在微信各种授权中可参考. w ...

随机推荐

  1. rabbitMQ+php

    RabbitMQ与PHP(一) 项目中使用RabbitMQ作为队列处理用户消息通知,消息由前端PHP代码产生,处理消息使用Python,这就导致代码一致性问题,调整消息定义时需要PHP和Python都 ...

  2. 编程模式之装饰模式(Decorator)

    装饰模式由四个角色组成:抽象组件角色,抽象装饰者角色,具体组件角色,具体装饰者角色. 抽象组件角色:给出一个抽象接口,以规范"准备接受附加功能"的对象. 抽象装饰者角色:持有一个组 ...

  3. mobx源码解读1

    mobx是redux的代替品,其本身就是一个很好的MVVM框架.因此花点力气研究一下它. 网上下最新的2.75 function Todo() { this.id = Math.random() mo ...

  4. type

    MollyPages.org"You were wrong case.To live here is to live." Home Pages / Database / Forms ...

  5. Waitforsingleobject 函数的用法

    用户模式的线程同步机制效率高,如果需要考虑线程同步问题,应该首先考虑用户模式的线程同步方法. 但是,用户模式的线程同步有限制,对于多个进程之间的线程同步,用户模式的线程同步方法无能为力.这时,只能考虑 ...

  6. Sql Server隔离级别(1)

    数据库是一个并发操作的环境,就像多线程一样,这样在高并发的情况下回出现一些问题. 假设我们有一张表Account,表结构和数据如下所示 AccountName Balance jo 100 fo 20 ...

  7. 笔记9-徐 DBCC SHRINKFILE不起作用的原因

    1 , , , , , , , , ,40) ,1 page_id pg_alloc ext_size obj_id index_id partition_number partition_id ia ...

  8. String 与 StringBuffer的区别

    String="a" 的方式每相加一次就创建一个新的常量,原常量不消失,比较占内存:StringBuffer是放在堆里面,append直接在原地址相加,不占内存

  9. asp.net LINQ数据访问技术from where select order by子句

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  10. http 错误 500.21

    出现这个错误一般都是.net Framework 在iis中安装错误 这个时候需要重新安装iis .netframework 4.0框架 我的解决方案如下: 在cmd中以管理员身份运行->%wi ...