C#图片保存到本地
/// <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#图片保存到本地的更多相关文章
- php 获取远程图片保存到本地
php 获取远程图片保存到本地 使用两个函数 1.获取远程文件 2.把图片保存到本地 /** * 获取远程图片并把它保存到本地 * $url 是远程图片的完整URL地址,不能为空. */ functi ...
- iOS 将图片保存到本地
//将图片保存到本地 + (void)SaveImageToLocal:(UIImage*)image Keys:(NSString*)key { NSUserDefaults* prefer ...
- iOS-iOS调用相机调用相册【将图片保存到本地相册】
设置头部代理 <UINavigationControllerDelegate, UIImagePickerControllerDelegate> 1.调用相机 检测前置摄像头是否可用 - ...
- Android View转为图片保存为本地文件,异步监听回调操作结果;
把手机上的一个View或ViewGroup转为Bitmap,再把Bitmap保存为.png格式的图片: 由于View转Bitmap.和Bitmap转图片都是耗时操作,(生成一个1M的图片大约500ms ...
- js截图及绕过服务器图片保存至本地(html2canvas)
今天要分享的是用html2canvas根据自己的需求生成截图,并且修复html2canvas截图模糊,以及绕过服务器图片保存至本地. 只需要短短的几行代码,就能根据所需的dom截图,是不是很方便,但是 ...
- React Native之图片保存到本地相册(ios android)
React Native之图片保存到本地相册(ios android) 一,需求分析 1,react native保存网络图片到相册,iOS端可以用RN自带的CameraRoll完美解决,但是andr ...
- FFmpeg解码视频帧为jpg图片保存到本地
FFmpeg解码视频帧为jpg图片保存到本地 - CSDN博客 https://blog.csdn.net/qq_28284547/article/details/78151635
- java将base64解析图片保存到本地。
将base64解析图片保存到本地的两个方法 /** * base64转图片 * @param base64str base64码 * @param savePath 图片路径 * @return */ ...
- 2018-5-22-SublimeText-粘贴图片保存到本地
title author date CreateTime categories SublimeText 粘贴图片保存到本地 lindexi 2018-05-22 15:15:26 +0800 2018 ...
- 微信小程序图片保存到本地
微信小程序图片保存到本地是一个常用功能: 这里讲解下完整实现思路: 因为微信官方的授权只弹一次,用户拒绝后再次调用,就需要结合button组件的微信开放能力来调起,以下方案在微信各种授权中可参考. w ...
随机推荐
- 广东地区电信官方DNS服务器
以下是广东地区电信官方DNS服务器,简单记录,以备后用! 主解析服务器: 202.96.128.143 202.96.128.68 202.105.80.210 缓存服务器(亦可作DNS解析之用) c ...
- android 多线程下载
看代码: public class MainActivity extends AppCompatActivity { private final String TAG = MainActivity.c ...
- (转)javascript异步编程的四种方法
本文转自:http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html 作者:阮一峰 本文仅仅作为个人mark ...
- eclipse连接外部tomcat进行debug
首先,在tomcat/bin目录下找到编辑catalina.bat,在 rem $Id: catalina.bat 1344732 2012-05-31 14:08:02Z kkolinko $rem ...
- HTML5 CANVAS画图 beginPath和closePath
beginPath这个canvas函数我很早就讲过了,他的作用很简单,就是开始一段新路径,我们先来看下面的一小段代码: var ctx = document.getElementById('cvs') ...
- TMS320DM642启动模式(Bootmode)(转)
原文地址:http://www.cnblogs.com/xiangai10000/p/JamesYang.html 在TI官方的文档<TMS320DM642 Video/Imaging Fixe ...
- 数字信号处理--FFT
FFT是离散傅立叶变换的快速算法,可以将一个信号变换到频域.有些信号在时域上是很难看出什么特征的,但是如果变换到频域之后,就很容易看出特征了.这就是很多信号分析采用FFT变换的原因.另外,FFT可以将 ...
- 微软要支持Objective-C了
今天的新闻,见http://www.solidot.org/story?sid=43899 更详细的见,http://arstechnica.com/information-technology/20 ...
- Storyboard中segue(转场)使用
源引:http://www.2cto.com/kf/201210/161737.html 一.视图切换类型介绍在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,s ...
- jquery取框架当前的url
前提,当前frame无名或动态生成的: var url = eval($("#ifr1").attr("name")+".location.href; ...