首页 > 代码库 > 服务器端上传功能的一般处理程序,支持远程提交上传

服务器端上传功能的一般处理程序,支持远程提交上传

public class cl_file_upload : IHttpHandler{    public void ProcessRequest(HttpContext context)    {        context.Response.ContentType = "text/plain";        context.Response.Charset = "UTF-8";        UploadFileResult result = new UploadFileResult();        string savePath = "/attem";        string dirPath = context.Server.MapPath(savePath);        HttpPostedFile PostedFile = context.Request.Files[0];        // 读取原始文件名        string LocalName = PostedFile.FileName;        // 初始化byte长度.        byte[] File = new Byte[PostedFile.ContentLength];        // 转换为byte类型        System.IO.Stream stream = PostedFile.InputStream;        string filename, error;        if (CheckFile(File, "jpg,jpeg,gif,png,doc,docx,xls,xlsx,ppt,pptx,pdf", LocalName, 4097152, "1", out error, out filename))        {            string path = dirPath + @"\" + filename;            context.Request.Files[0].SaveAs(path);            result.result = 1;            result.url = savePath + "/" + filename;            result.localname = jsonString(LocalName);        }        else        {            result.result = 0;            result.error = error;        }        context.Response.Write(JsonHelp.ObjToJsonStr(result));    }    string jsonString(string str)    {        str = str.Replace("\\", "\\\\");        str = str.Replace("/", "\\/");        str = str.Replace("‘", "\\‘");        return str;    }    string GetFileExt(string FullPath)    {        if (FullPath != "") return FullPath.Substring(FullPath.LastIndexOf(‘.‘) + 1).ToLower();        else return "";    }    void CreateFolder(string FolderPath)    {        if (!System.IO.Directory.Exists(FolderPath)) System.IO.Directory.CreateDirectory(FolderPath);    }    /// <summary>    /// 检测上传文件的有效性    /// </summary>    /// <param name="file">文件</param>    /// <param name="file_ext">允许上传的扩展名</param>    /// <param name="localname">本地文件名称,带扩展名</param>    /// <param name="max_size">允许上传文件最大大小</param>    /// <param name="error">错误信息</param>    /// <param name="filename">最终生成的文件名</param>    /// <returns></returns>    bool CheckFile(byte[] file, string file_ext, string localname, int max_size, string CreateFileNameType, out string error, out string filename)    {        error = "";        filename = "";        if (file.Length == 0)        {            error = "无数据提交";            return false;        }        if (file.Length > max_size)        {            error = "文件大小超过" + max_size + "字节";            return false;        }        // 取上载文件后缀名        string extension = GetFileExt(localname);        if (("," + file_ext + ",").IndexOf("," + extension + ",") < 0)        {            error = "上传文件扩展名必需为:" + file_ext;            return false;        }        if (CreateFileNameType == "1")        {            Random random = new Random(DateTime.Now.Millisecond);            filename = DateTime.Now.ToString("yyyyMMddhhmmss") + random.Next(10000) + "." + extension;        }        else        {            filename = localname;        }        return true;    }    /// <summary>    ///     /// </summary>    /// <param name="create_originalImageName">是否保存原图</param>    /// <param name="originalImage">原图</param>    /// <param name="imageServerRootDirectory">保存根目录</param>    /// <param name="imageSubDirectory">图片目录</param>    /// <param name="originalImageName">原图新名称</param>    /// <param name="thumbnailName_small">缩略小图名称</param>    /// <param name="maxSize_small">缩略小图最大尺寸</param>    /// <param name="thumbnailName_big">缩略小图名称</param>    /// <param name="maxSize_big">缩略大图最大尺寸</param>    /// <returns></returns>    protected bool Make_thumbnail_Image_big_small(bool create_originalImageName, System.Drawing.Image originalImage, string imageServerRootDirectory, string imageSubDirectory, string originalImageName, string thumbnailName_small, int maxSize_small, string thumbnailName_big, int maxSize_big)    {        string thumbnailPath = imageServerRootDirectory + "\\" + imageSubDirectory + "\\" + thumbnailName_big;        string originalImagePath = imageServerRootDirectory + "\\" + imageSubDirectory + "\\" + originalImageName;        int x = 0;        int y = 0;        int ow = originalImage.Width;        int oh = originalImage.Height;        int towidth = 0;        int toheight = 0;        if (ow < maxSize_big && oh < maxSize_big)        {            towidth = ow;            toheight = oh;        }        else        {            #region 计算比例            if (ow > oh) // 宽 比 高    大            {                decimal YuanShiBiLi = (decimal)oh / (decimal)ow;                towidth = (ow > maxSize_big) ? maxSize_big : ow;                toheight = (int)(YuanShiBiLi * towidth);            }            else            {                decimal BiLi = (decimal)ow / (decimal)oh;                toheight = (oh > maxSize_big) ? maxSize_big : oh;                towidth = (int)(toheight * BiLi);            }            #endregion        }        #region  大图        //新建一个bmp图片          System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);        //新建一个画板          System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);        //设置高质量插值法          g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;        //设置高质量,低速度呈现平滑程度          g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;        //清空画布并以透明背景色填充          g.Clear(System.Drawing.Color.Transparent);        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),//在指定位置并且按指定大小绘制原图片的指定部分              new System.Drawing.Rectangle(x, y, ow, oh),            System.Drawing.GraphicsUnit.Pixel);        try        {            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);  //以jpg格式保存缩略图          }        catch (System.Exception e)        {            throw e;        }        #endregion        if (thumbnailName_small.Length > 2)        {            #region small 小图            {                int x_small = 0;                int y_small = 0;                int ow_small = towidth;                int oh_small = toheight;                int towidth_small = 0;                int toheight_small = 0;                if (ow_small > oh_small)                {                    decimal bili_small = (decimal)oh_small / (decimal)ow_small;                    towidth_small = (ow_small > maxSize_small) ? maxSize_small : ow_small;                    toheight_small = (int)(bili_small * towidth_small);                }                else                {                    decimal bili_small = (decimal)ow_small / (decimal)oh_small;                    toheight_small = (oh_small > maxSize_small) ? maxSize_small : oh_small;                    towidth_small = (int)(bili_small * toheight_small);                }                //新建一个bmp图片                  System.Drawing.Image bitmap_small = new System.Drawing.Bitmap(towidth_small, toheight_small);                //新建一个画板                  System.Drawing.Graphics g_small = System.Drawing.Graphics.FromImage(bitmap_small);                //设置高质量插值法                  g_small.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                //设置高质量,低速度呈现平滑程度                  g_small.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;                //清空画布并以透明背景色填充                  g_small.Clear(System.Drawing.Color.Transparent);                System.Drawing.Image originalImage_small = bitmap;                g_small.DrawImage(originalImage_small, new System.Drawing.Rectangle(0, 0, towidth_small, toheight_small),//在指定位置并且按指定大小绘制原图片的指定部分                    new System.Drawing.Rectangle(x_small, y_small, ow_small, oh_small),                  System.Drawing.GraphicsUnit.Pixel);                string thumbnailPath_small = imageServerRootDirectory + "\\" + imageSubDirectory + "\\" + thumbnailName_small;                try                {                    bitmap_small.Save(thumbnailPath_small, System.Drawing.Imaging.ImageFormat.Jpeg);  //以jpg格式保存缩略图                  }                catch (System.Exception e)                {                    throw e;                }                finally                {                    originalImage_small.Dispose();                    bitmap_small.Dispose();                    g_small.Dispose();                }            }            #endregion        }        if (create_originalImageName == true)        {            originalImage.Save(originalImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);        }        try        {            originalImage.Dispose();            bitmap.Dispose();            g.Dispose();        }        catch (Exception ex)        {        }        return true;    }    public bool IsReusable    {        get        {            return false;        }    }}

 

服务器端上传功能的一般处理程序,支持远程提交上传