向服务器post或者get数据返回

        #region 向服务器端Get值返回
        /// <summary>
        /// 向服务器端Get返回
        /// </summary>
        ///<see cref="Author">小柯</see> 
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <param name="param">GET</param>
        /// <returns></returns>
        public static string SendInformation(string Url, string Param, string Method = "GET")
        {           
            if (!string.IsNullOrEmpty(Param))
            {
                Url = Url + "?" + Param;                
            }
            Uri uri = new Uri(Url);
            HttpWebRequest webReq = HttpWebRequest.Create(uri) as System.Net.HttpWebRequest;
            webReq.Method = Method;
            webReq.Headers["Cache-Control"] = "no-cache";
            HttpWebResponse webResp = null;
            Stream respStream = null;
            StreamReader objReader = null;
            try
            {
                webResp = (HttpWebResponse)webReq.GetResponse();
                respStream = webResp.GetResponseStream();
                objReader = new StreamReader(respStream, System.Text.Encoding.GetEncoding("UTF-8"));
                return objReader.ReadToEnd();
            }
            catch (WebException ex)
            {
                return "无法连接到服务器/r/n错误信息:" + ex.Message;
            }
            finally
            {
                if (webResp != null)
                    webResp.Close();
                if (respStream != null)
                    respStream.Close();
                if (objReader != null)
                    objReader.Close();
            }
        }
        #endregion

        #region 向服务器端post返回
        /// <summary>
        /// 向服务器端post返回
        /// </summary>
        /// <param name="url">服务器URL</param>
        /// <param name="param">要发送的参数字符串</param>
        /// <returns>服务器返回字符串</returns>
        public static string PostInformation(string url, string param, string Method="Post")
        {
            System.Text.Encoding myEncode = System.Text.Encoding.GetEncoding("UTF-8");
            byte[] postBytes = System.Text.Encoding.ASCII.GetBytes(param);
            HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
            webReq.Headers["Cache-Control"] = "no-cache";
            webReq.Method = Method;
            webReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            webReq.ContentLength = postBytes.Length;
            Stream reqStream = null;
            HttpWebResponse webResp = null;
            StreamReader sr = null;
            try
            {
                reqStream = webReq.GetRequestStream();
                reqStream.Write(postBytes, 0, postBytes.Length);
                webResp = (HttpWebResponse)webReq.GetResponse();
                sr = new StreamReader(webResp.GetResponseStream(), myEncode);
                return sr.ReadToEnd();
            }
            catch (WebException ex)
            {
                return "无法连接到服务器\r\n错误信息:" + ex.Message;
            }
            finally
            {
                if (reqStream != null)
                    reqStream.Close();
                if (webResp != null)
                    webResp.Close();
                if (sr != null)
                    sr.Close();
            }
        }
        #endregion


 

        /// <summary>
        ///  远程上传图片
        /// <param name="fileData">文件数据</param>
        /// <param name="ImgName">文件名</param>
        /// <param name="ImgPath">图片放的文件路径</param>
        public static bool RemoteUpload(byte[] fileData, string ImgName, string ImgPath)
        {
            try
            {
                string _CustomEncryption = CustomEncryption();
                if (fileData.Length == 0) return false;
                if (string.IsNullOrEmpty(ImgPath)) { ImgPath = "/Upload/User/" + PublicFun.MemberID(); }
                System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri(System.Configuration.ConfigurationManager.AppSettings["Resource_Url"] + "/Programs/MyGallery/UploadImg.ashx?CustomEncryption=" + _CustomEncryption + "&ImgName=" + ImgName + "&ImgPath=" + ImgPath)) as System.Net.HttpWebRequest;
                request.Headers["Cache-Control"] = "no-cache";
                request.Method = "POST";
                request.ContentLength = fileData.Length;// 设置请求参数的长度.

                Stream stream = request.GetRequestStream();// 取得发向服务器的流
                stream.WriteTimeout = 10000;
                stream.Write(fileData, 0, fileData.Length);
                stream.Flush();
                stream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                Stream htmlstream = response.GetResponseStream();
                StreamReader sr = new StreamReader(htmlstream);
                string htmlcode = sr.ReadToEnd();//获得服务器返回内容
                // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                sr.Close();
                htmlstream.Close();
                response.Close();
                if (htmlcode == "上传成功!")
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("文件上传失败", ex.InnerException);
            }
        }
        /// <summary>
        ///  远程上传图片
        /// </summary>
        /// <param name="fileData">文件数据</param>
        /// <param name="ImgName">文件名</param>
        /// <param name="UploadUrl">服务器地址</param>
        /// <param name="Width">要求图片的宽</param>
        /// <param name="Height">要求图片的高</param>
        public static bool RemoteUpload(byte[] fileData, string ImgName, string ImgPath, double Width, double Height)
        {
            try
            {
                string _CustomEncryption = CustomEncryption();
                if (fileData.Length == 0) return false;
                if (string.IsNullOrEmpty(ImgPath)) { ImgPath = "/Upload/User/" + PublicFun.MemberID(); }
                System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri(System.Configuration.ConfigurationManager.AppSettings["Resource_Url"] + "/Programs/MyGallery/UploadImg.ashx?CustomEncryption=" + _CustomEncryption + "&ImgName=" + ImgName + "&ImgPath=" + ImgPath + "&Width=" + Width + "&Height=" + Height)) as System.Net.HttpWebRequest;
                request.Headers["Cache-Control"] = "no-cache";
                request.Method = "POST";
                request.ContentLength = fileData.Length;// 设置请求参数的长度.

                Stream stream = request.GetRequestStream();// 取得发向服务器的流
                stream.WriteTimeout = 10000;
                stream.Write(fileData, 0, fileData.Length);
                stream.Flush();
                stream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                Stream htmlstream = response.GetResponseStream();
                StreamReader sr = new StreamReader(htmlstream);
                string htmlcode = sr.ReadToEnd();//获得服务器返回内容
                // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                sr.Close();
                htmlstream.Close();
                response.Close();
                if (htmlcode == "上传成功!")
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("文件上传失败", ex.InnerException);
            }
        }
        /// <summary>
        ///  远程删除图片
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="Message">输出信息</param>
        public static bool RemoteDelete_Img(string fileName, out string Message)
        {
            if (PublicFun.MemberID() > 0)
            {
                try
                {
                    if (fileName.IndexOf("://") > 0)
                    {
                        fileName = PublicFun.RemovalUrl(fileName);
                    }
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/Delete_Img.ashx?fileName=" + fileName)) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    Message = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                    if (Message == "删除成功!")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("文件删除失败!", ex.InnerException);
                }
            }
            else
            {
                Message = "请登录!";
                return false;
            }
        }
        /// <summary>
        ///  远程删除图片/文件夹
        /// </summary>
        /// <param name="fileName">文件名</param>
        public static bool RemoteDelete_File(string fileName)
        {
            int MemberID = PublicFun.MemberID();
            string Message = "";
            if (MemberID > 0)
            {
                try
                {
                    if (fileName.IndexOf("/Upload/User/") < 1)
                    {
                        fileName = "/Upload/User/" + MemberID + "/" + fileName;
                    }
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/Delete_Img.ashx?fileName=" + fileName)) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    Message = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();

                }
                catch (Exception ex)
                {
                    throw new Exception("文件删除失败!", ex.InnerException);
                }
            }
            if (Message == "删除成功!")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        ///  获取我的图库
        /// </summary>
        /// <param name="Message">输出信息</param>
        public static string GetMyGallery()
        {
            string Message = "";
            try
            {
                if (PublicFun.MemberID() > 0)
                {
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/GetMyGallery.ashx?MemberID=" + PublicFun.MemberID())) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    Message = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                }
                return Message;

            }
            catch (Exception ex)
            {
                throw new Exception("文件删除失败!", ex.InnerException);
            }
        }
        /// <summary>
        ///  创建文件夹
        /// </summary>
        /// <param name="FolderName">文件夹名称</param>
        /// <param name="Message">输出信息</param>
        public static string CreateFolder(string FolderName)
        {
            string Message = "";
            try
            {
                if (PublicFun.MemberID() > 0)
                {
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/CreateFolder.ashx?path=" + GetUploadPath(FolderName))) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    Message = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                }
                return Message;

            }
            catch (Exception ex)
            {
                throw new Exception("文件夹创建失败!", ex.InnerException);
            }
        }
        /// <summary>
        /// 文件重命名(包含文件夹)
        /// </summary>
        /// <param name="OldNmaePath">旧文件路径</param>
        /// <param name="NewName">新文件名称</param>
        /// <returns>文件重命名成功!(表示成功,其他消息表示失败)</returns> 
        public static string File_ReName(string OldNmaePath, string NewName)
        {
            string Message = "";
            try
            {
                if (PublicFun.MemberID() > 0)
                {
                    if (OldNmaePath.IndexOf("/Upload/User/") < 0)
                    {
                        OldNmaePath = "/Upload/User/" + PublicFun.MemberID() + "/" + OldNmaePath;
                    }
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/FileReName.ashx?OldNmaePath=" + GetUploadPath(OldNmaePath) + "&NewName=" + NewName)) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    Message = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                }
                return Message;

            }
            catch (Exception ex)
            {
                throw new Exception("文件重命名失败!", ex.InnerException);
            }
        }
        /// <summary>
        /// 获取图库文件夹以及文件夹内的文件数
        /// </summary>
        /// <returns>(文件夹1,文件数/文件夹2,文件数.....)</returns>
        public static string MyGalleryFolder()
        {
            string folder = "";
            try
            {
                if (PublicFun.MemberID() > 0)
                {
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/MyGalleryFolder.ashx?MemberID=" + PublicFun.MemberID())) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    folder = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                }
                return folder;

            }
            catch (Exception ex)
            {
                throw new Exception("获取失败!", ex.InnerException);
            }
        }
        /// <summary>
        /// 获取图库文件夹的文件
        /// </summary>
        /// <param name="Folder">文件夹名称</param>
        /// <returns>文件,文件,......</returns>
        public static string GetFolderFiles(string Folder)
        {
            string folder = "";
            try
            {
                int MemberID = PublicFun.MemberID();
                if (MemberID > 0)
                {
                    string FolderPath = MemberID + "/" + Folder;
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/FolderFiles.ashx?FolderPath=" + FolderPath)) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    folder = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                }
                return folder;

            }
            catch (Exception ex)
            {
                throw new Exception("获取失败!", ex.InnerException);
            }
        }
        /// <summary>
        /// 获取完整上传路径
        /// </summary>
        /// <param name="Path">原始路径</param>
        /// <returns>返回完整路径</returns>
        public static string GetUploadPath(string Path)
        {
            string NewPath = "Upload/User/" + PublicFun.MemberID() + "/";
            if (!string.IsNullOrEmpty(Path))
            {
                if (Path.IndexOf(NewPath) > 0)
                {
                    NewPath = Path;
                }
                else
                {
                    NewPath += Path;
                }
            }
            return NewPath;

        }
        /// <summary>
        /// 设置封面图片
        /// </summary>
        /// <param name="SourcePic">图片来源</param>
        /// <param name="Folder">设置的文件夹</param>
        /// <returns>bool</returns>
        public static bool SetupCover(string SourcePic, string Folder)
        {
            string folder = "";
            try
            {
                int MemberID = PublicFun.MemberID();
                if (MemberID > 0)
                {
                    string FolderPath = MemberID + "/" + Folder;
                    byte[] fileData = new byte[1];
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/MyGallery/SetupCover.ashx?FilePath=" + SourcePic + "&Folder=" + FolderPath)) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "POST";
                    request.ContentLength = fileData.Length;// 设置请求参数的长度.

                    Stream stream = request.GetRequestStream();// 取得发向服务器的流
                    stream.WriteTimeout = 10000;
                    stream.Write(fileData, 0, fileData.Length);
                    stream.Flush();
                    stream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    folder = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                }
                if (folder.Trim() == "设置成功!")
                {
                    return true;
                }
                else
                    return false;

            }
            catch (Exception ex)
            {
                throw new Exception("获取失败!", ex.InnerException);
            }
        }
        /// <summary>
        /// 删除文件重命名文件
        /// 王泳涛
        /// </summary>
        /// <param name="newpath">新的图片地址</param>
        /// <param name="path">老的图片地址</param>
        /// <returns></returns>
        public static bool Delete(string newpath, string path)
        {
            string Message = "";
            try
            {
                if (PublicFun.MemberID() > 0)
                {
                    System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(new Uri("http://localhost:94/Programs/Delete.ashx?path=" + path + "&newpath=" + newpath)) as System.Net.HttpWebRequest;
                    request.Headers["Cache-Control"] = "no-cache";
                    request.Method = "GET";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();// GetResponse 方法才真的发送请求,等待服务器返回
                    Stream htmlstream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(htmlstream);
                    Message = sr.ReadToEnd();//获得服务器返回内容
                    // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
                    sr.Close();
                    htmlstream.Close();
                    response.Close();
                }
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }


<%@ WebHandler Language="C#" Class="UploadImg" %>

using System;
using System.Web;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Text;
using System.Reflection;

public class UploadImg : VerifyCode, IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string fPath = context.Request.QueryString["ImgPath"];
        string fName = context.Request.QueryString["ImgName"];
        string Width = context.Request.QueryString["Width"];
        string Height = context.Request.QueryString["Height"];
        if (fName.IndexOf(".") < 1)
        {
            fName += ".jpg";
        }
        if (!string.IsNullOrEmpty(fPath) && !string.IsNullOrEmpty(fName))
        {
            var strem = HttpContext.Current.Request.InputStream;
            long len = strem.Length;
            if (len == 0)
            {
                strem.Close();
                context.Response.ContentType = "text/plain";
                context.Response.Write("传输错误!");
                return;

            }
            else
            {
                byte[] bytes = new byte[len];
                string path = System.Web.HttpContext.Current.Server.MapPath("~/" + HttpUtility.UrlDecode(fPath));
                int count = strem.Read(bytes, 0, bytes.Length);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                FileStream fStream = new FileStream(path + "/" + fName, FileMode.Create);
                fStream.Write(bytes, 0, count);
                fStream.Flush();
                fStream.Close();
                if (!string.IsNullOrEmpty(Width) && !string.IsNullOrEmpty(Height))
                {
                    try
                    {
                        double targetWidth = Convert.ToDouble(Width);
                        double targetHeight = Convert.ToDouble(Height);
                        //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
                        System.Drawing.Image initImage = System.Drawing.Image.FromStream(strem, true);
                        //原图宽高均小于模版,不作处理,直接保存

                        //缩略图宽、高计算
                        double newWidth = initImage.Width;
                        double newHeight = initImage.Height;
                        //宽大于高或宽等于高(横图或正方)
                        if (initImage.Width < initImage.Height || initImage.Width == initImage.Height)
                        {
                            //如果宽大于模版
                            if (initImage.Width > targetWidth)
                            {
                                //宽按模版,高按比例缩放
                                newWidth = targetWidth;
                                newHeight = initImage.Height * (targetWidth / initImage.Width);
                            }
                        }
                        //高大于宽(竖图)
                        else
                        {
                            //如果高大于模版
                            if (initImage.Height > targetHeight)
                            {
                                //高按模版,宽按比例缩放
                                newHeight = targetHeight;
                                newWidth = initImage.Width * (targetHeight / initImage.Height);
                            }
                        }
                        //生成新图
                        //新建一个bmp图片
                        System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
                        //新建一个画板
                        System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
                        //设置质量
                        newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        //置背景色
                        newG.Clear(Color.White);
                        //画图
                        newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);

                        //保存缩略图
                        //newImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        newImage.Save(path + "\\s_" + fName);
                        //释放资源
                        newG.Dispose();
                        newImage.Dispose();
                        initImage.Dispose();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                }
                strem.Close();
                context.Response.ContentType = "text/plain";
                context.Response.Write("上传成功!");
            }
        }
        else
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("传输错误!");
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

 

<%@ WebHandler Language="C#" Class="UploadAdImg" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Net;
using Maticsoft.Common;
using System.Web.SessionState;

public class UploadAdImg : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Charset = "utf-8";
        //string sessionid = context.Request.QueryString["sessionid"];
        if (PublicFun.MemberID() < 1)
        {
            context.Response.Write("0");
            return; 
        }
        HttpPostedFile file = context.Request.Files["FileData"];
        //string UploadUrl = context.Request.QueryString["uploadurl"] ?? "http://localhost:94/Programs/UploadImg.ashx";//上传Url
        string ImgPath = context.Request.QueryString["uploadpath"];//上传的路劲(从根开始文件夹路径)
        //自定义命名
        string ImgName =context.Request.QueryString["filename"]??string.Format("{0}", DateTime.Now.ToString("yyyyMMddhhmmssfff"));
        ImgName +=PublicFun.GetImageType(file.FileName);
        int id = -1;
        int.TryParse(context.Request.QueryString["id"], out id);
        if (file != null)
        {
            byte[] bytes = new byte[file.InputStream.Length];
            file.InputStream.Read(bytes,0,bytes.Length);
            file.InputStream.Close();
            bool Istrue = false;
            double width =0;
            double height =0;
            if (id == 1)
            {
                width = 210;
                height = 210; 
            }
            else if (id > 1)
            {
                width = 60;
                height = 60; 
            }
            if (width>0 && height>0)
            {
                Istrue = Maticsoft.Common.PublicFun.RemoteUpload(bytes, ImgName, ImgPath, width, height);
            }
            else
            {
                Istrue = Maticsoft.Common.PublicFun.RemoteUpload(bytes, ImgName, ImgPath);
            }
            if (Istrue)//通过流上传资源站上传页面
            {
                string strurl = PublicVal.Resources_Url;//获取根目录
               
                if (id>0)
                {
                    context.Response.Write( "/Upload/User/"+PublicFun.MemberID()+"/" + ImgName + "|" + (id-1).ToString()); //返回图片的src 
                }
                else
                {
                    context.Response.Write(ImgPath + "/" + ImgName); //返回图片的src
                }
            }
            else
            {
                context.Response.Write("0");
            }
        }
        else
        {
            context.Response.Write("0");
        }
        context.Response.End();
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

 

posted @ 2015-07-06 15:16  天涯过者  阅读(708)  评论(0编辑  收藏  举报