1.上传文件,在一般处理程序中处理

 //1.接收post过来的文件
HttpPostedFile file = context.Request.Files[];
if (file.ContentLength > || file.ContentLength > )
{
if (file.ContentType.Contains("image/"))
{
//2.讲文件转换为文件流
Stream stream = file.InputStream;
//3.创建容纳文件流的byte数组
byte[] buff = new byte[stream.Length];
//4.将流存储进byte数组中
stream.Read(buff, , buff.Length);
//5.将byte数组转换为base64字符串
string strParam = Convert.ToBase64String(buff);
//6.调用webService方法,传入文件字符串
srImg.wsImgSoapClient cl = new srImg.wsImgSoapClient();
int result = cl.AddFile(strParam);
if (result == )
{
context.Response.Write("上传成功");
}
}
else
{
context.Response.Write("您上传的不是图片类型");
}
}
else
{
context.Response.Write("您上传的文件大小错误");
}

ashx后台处理上传文件

2.在webService方法中做如下处理:

 #region 通过流保存图片文件
/// <summary>
/// 通过流保存图片文件
/// </summary>
/// <param name="streamStr">文件流字符串</param>
/// <returns></returns>
[WebMethod]
public int AddFile(string streamStr)
{
//1.将文件字符流转换为byte数组
byte[] imageBytes = Convert.FromBase64String(streamStr);
//2.根据数组获取存储区内存流
MemoryStream stream = new MemoryStream(imageBytes);
//3.根据file获取stream流,然后根据流生成image对象
using (Image img = Image.FromStream(stream))
{
//设置压缩率
double rate = ;
//开始压缩文件
return ThumImg(img, rate);
}
}

webservice

3.通用简单压缩方法,需要搜索更好的优化方法

 #region 压缩文件
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="img">图片对象</param>
/// <param name="rate">压缩率</param>
/// <returns></returns>
public int ThumImg(Image img, double rate)
{
//1.创建新的压缩位图
using (Image thumImg = new Bitmap((int)(img.Width * rate), (int)(img.Height * rate)))
{
using (Graphics g = Graphics.FromImage(thumImg))
{
//2.在压缩图片按原图画出缩小版的图
g.DrawImage(img, new Rectangle(, , thumImg.Width, thumImg.Height), new Rectangle(, , img.Width, img.Height), GraphicsUnit.Pixel);
}
//3.存储图片文件
string phyPath = System.Web.HttpContext.Current.Server.MapPath("/upload/");
thumImg.Save(phyPath + Guid.NewGuid().ToString() + "." + GetImageFormat(thumImg), System.Drawing.Imaging.ImageFormat.Jpeg);
return ;
}
}
#endregion

图片压缩方法

4.通过image对象判断图片后缀方法

 #region 返回图片格式(和文件名后缀无关)
/// <summary>
/// 返回图片格式(和文件名后缀无关)
/// </summary>
/// <param name="strImgPath">图片路径及名称</param>
/// <returns>jpeg\gif\bmp\png\tif\icon\wmf</returns>
string GetImageFormat(Image imgSrc)
{
string strImgFormat = "jpg";
if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
strImgFormat = "jpeg";
else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
strImgFormat = "gif";
else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
strImgFormat = "bmp";
else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
strImgFormat = "png";
else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
strImgFormat = "tiff";
else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
strImgFormat = "icon";
else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
strImgFormat = "wmf";
return strImgFormat;
}
#endregion

通过image对象判断后缀

webservice跨域上传图片的更多相关文章

  1. ueditor富文本编辑器跨域上传图片解决办法

    在使用百度富文本编辑器上传图片的过程中,如果是有一台单独的图片服务器就需要将上传的图片放到图片服务器,比如在a.com的编辑器中上传图片,图片要保存到img.com,这就涉及到跨域上传图片,而在ued ...

  2. JS-JQuery(JSONP)调用WebService跨域若干技术点

    1.JSONP:JSON With Padding,让网页从别的网域获取信息,也就是跨域获取信息,可以当做是一种“工具”,大多数架构Jquery.EXTjs等都支持. 由于同源策略,一般来说位于 se ...

  3. 项目二(业务GO)——跨域上传图片(请求接口)

    之前,就听过“跨域上传”图片的问题,只是疏于研究,也就一再搁置,直至今天再次遇见这个不能避免的“坑”,才不得不思考一下,怎么“跨域上传”图片或者文件? 问题来源: 何为“跨域”? ——就是给你一个接口 ...

  4. Ajax请求WebService跨域问题 [转载]

    1.背景 用Jquery中Ajax方式在asp.net开发环境中WebService接口的调用 2.出现的问题 原因分析:浏览器同源策略的影响(即JavaScript或Cookie只能访问同域下的内容 ...

  5. 解决api、WebService跨域问题

    webapi接口在ajax调用的很多情况下都会出现跨域问题,同样的WebService如果想用ajax调用,也需要接口跨域问题,解决方案如下: 1.IIS配置 打开IIS选择发布后的webapi或者是 ...

  6. Sliverlight调用WebService跨域问题解决

    在SilverlightApplication正常添加webservice(承载网站中建webservice,这样就不存在跨域问题了,即域名一样如:localhost:4676) http://loc ...

  7. django —— KindEditor - 跨域上传图片

    #跨域上传方法 def frontupload(request): if request.method == 'POST': item = {} file = request.FILES.get('i ...

  8. ajax访问WebService跨域问题

    1.先看一个网站介绍,了解跨域问题    HTTP访问控制(CORS) 2.像谷歌.火狐浏览器对一些非简单请求会触发预检请求,首先使用 OPTIONS   方法发起一个预检请求到服务器,然而IE浏览器 ...

  9. ajax调用webservice 跨域问题

    用js或者jquery跨域调用接口时 对方的接口需要做jsonp处理,你的ajax jsonp调用才可以 egg 接口中已经做了jsonp处理,所以可以跨域调用 //$.ajax({ // url: ...

随机推荐

  1. C# 常用函数和方法集

    1.DateTime 数字型  System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System. ...

  2. java 操作配置文件 .properties

    package com.dms.common; import java.io.File; import java.io.FileInputStream; import java.io.FileNotF ...

  3. 2016 Multi-University Training Contest 4 总结

    第四场多校队伍的发挥还是相当不错的. 我倒着看题,发觉最后一题树状数组可过,于是跟队友说,便开始写,十分钟AC. 欣君翻译01题给磊哥,发现是KMP裸题,但是发现模板太旧,改改后过了. 11题是一道毒 ...

  4. LDAP Authentication for openNebula3.2

    LDAP Authentication 3.2 The LDAP Authentication addon permits users to have the same credentials as ...

  5. js 事件之 createEvent、dispatchEvent

    //document上绑定自定义事件ondataavailable document.addEventListener('customevent', function(event) { alert(e ...

  6. dataset 用法(3)

    ReadXml 提供了只将数据或同时将数据和架构从 XML 文档读入 DataSet 的方式(若要同时读数据和架构,请使用包括 mode 参数的 ReadXML 重载之一,并将其值设置为 ReadSc ...

  7. xcode UIButton创建、监听按钮点击、自定义按钮 、状态 、内边距

    代码创建 //创建UIButton UIButton * btnType=[[UIButton alloc]init]; //设置UIControlStateNormal状态下的文字颜色 [btnTy ...

  8. select实现选中跳转

    select选择后直接跳转到其他网站的三种方式     第一种: ************************** <html> <head> <meta http- ...

  9. MYSQL知识点

    1.MYSQL为了可移植性,使用"--"做为注释,使用"/*!*/"表示可执行的注释.

  10. 3种方式实现可滑动的Tab

    1. 第一种,使用 TabHost + ViewPager 实现 该方法会有一个Bug,当设置tabHost.setCurrentTab()为0时,ViewPager不显示(准确的说是加载),只有点击 ...