zoukankan      html  css  js  c++  java
  • webservice跨域上传图片

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

     1 //1.接收post过来的文件
     2 HttpPostedFile file = context.Request.Files[0];
     3 if (file.ContentLength > 0 || file.ContentLength > 1024)
     4 {
     5     if (file.ContentType.Contains("image/"))
     6     {
     7         //2.讲文件转换为文件流
     8         Stream stream = file.InputStream;
     9         //3.创建容纳文件流的byte数组
    10         byte[] buff = new byte[stream.Length];
    11         //4.将流存储进byte数组中
    12         stream.Read(buff, 0, buff.Length);
    13         //5.将byte数组转换为base64字符串
    14         string strParam = Convert.ToBase64String(buff);
    15         //6.调用webService方法,传入文件字符串
    16         srImg.wsImgSoapClient cl = new srImg.wsImgSoapClient();
    17         int result = cl.AddFile(strParam);
    18         if (result == 0)
    19         {
    20             context.Response.Write("上传成功");
    21         }
    22     }
    23     else
    24     {
    25         context.Response.Write("您上传的不是图片类型");
    26     }
    27 }
    28 else
    29 {
    30     context.Response.Write("您上传的文件大小错误");
    31 }
    ashx后台处理上传文件

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

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

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

     1 #region 压缩文件
     2 /// <summary>
     3 /// 压缩文件
     4 /// </summary>
     5 /// <param name="img">图片对象</param>
     6 /// <param name="rate">压缩率</param>
     7 /// <returns></returns>
     8 public int ThumImg(Image img, double rate)
     9 {
    10     //1.创建新的压缩位图
    11     using (Image thumImg = new Bitmap((int)(img.Width * rate), (int)(img.Height * rate)))
    12     {
    13         using (Graphics g = Graphics.FromImage(thumImg))
    14         {
    15             //2.在压缩图片按原图画出缩小版的图
    16             g.DrawImage(img, new Rectangle(0, 0, thumImg.Width, thumImg.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
    17         }
    18         //3.存储图片文件
    19         string phyPath = System.Web.HttpContext.Current.Server.MapPath("/upload/");
    20         thumImg.Save(phyPath + Guid.NewGuid().ToString() + "." + GetImageFormat(thumImg), System.Drawing.Imaging.ImageFormat.Jpeg);
    21         return 0;
    22     }
    23 } 
    24 #endregion
    图片压缩方法

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

     1 #region 返回图片格式(和文件名后缀无关)
     2 /// <summary>
     3 /// 返回图片格式(和文件名后缀无关)
     4 /// </summary>
     5 /// <param name="strImgPath">图片路径及名称</param>
     6 /// <returns>jpeg\gif\bmp\png\tif\icon\wmf</returns>
     7 string GetImageFormat(Image imgSrc)
     8 {
     9     string strImgFormat = "jpg";
    10     if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
    11         strImgFormat = "jpeg";
    12     else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
    13         strImgFormat = "gif";
    14     else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
    15         strImgFormat = "bmp";
    16     else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
    17         strImgFormat = "png";
    18     else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
    19         strImgFormat = "tiff";
    20     else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
    21         strImgFormat = "icon";
    22     else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
    23         strImgFormat = "wmf";
    24     return strImgFormat;
    25 } 
    26 #endregion
    通过image对象判断后缀
  • 相关阅读:
    北航软院2012级C#期末考试部分考题解答
    题目1013:开门人和关门人(字符串处理)
    char * 与char []探究理解
    题目1017:还是畅通工程(最小生成树)
    最小生成树(Prim算法+Kruskal算法)
    题目1018:统计同成绩学生人数(hash简单应用)
    GoogLeNet InceptionV2/V3/V4
    Python库
    卷积为什么如此强大?一文全解深度学习中的卷积
    神经网络训练tricks
  • 原文地址:https://www.cnblogs.com/notniu/p/4096465.html
Copyright © 2011-2022 走看看