码迷,mamicode.com
首页 > Web开发 > 详细

数往知来 JQuery 缩略图_客户端文件下载<二十四>

时间:2015-09-10 00:29:03      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

一、缩略图

--》首先找到图片、或和用户的上传结合在一起

--》创建一个画布  与之前不同的是我们的画布给它一个固定的大小

-->创建一个画笔

--》

context.Response.ContentType = "image/jpeg";

        //得到图片的路径

        string filePath = context.Server.MapPath("image/1.jpg");

        //读取图片

        using (Image img = Image.FromFile(filePath))

        {

            //创建一个画布,固定尺寸(也就是缩略图的尺寸)

            using (Bitmap map = new Bitmap(200, 300))

            {

                //创建画笔

                using (Graphics g = Graphics.FromImage(map))

                {

                    //把图片画到画布上,注意第二个参数是设置的画布的宽和高

                    g.DrawImage(img, new Rectangle(0, 0, map.Width, map.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);

                    //输出

                    map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                }

            }

        }

二、文件下载

context.Response.ContentType = "text/html";

        //获得客户端请求的下载文件名

        string downFileName=context .Request.QueryString["name"];

        if (!string .IsNullOrEmpty(downFileName))

        {

            //判断客户端请求下载的文件在服务端是否存在

            //先获得该文件的全路径

            string filePath = context.Server.MapPath(downFileName);

            if (File.Exists(filePath))

            {

                //对文件爱你路径进行转码,防止向客户端输出时乱码

                string file=context.Server.UrlEncode(filePath);

                //想用户弹出一个保存对话框

                context.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + file + "\"");

                //如果存在,通过writeFile(_)方法向客户端输出文件

                context.Response.WriteFile(filePath);

            }

            else

            {

                context.Response.Write("文件不存在,请确定文件的路径是否正确");

            }

        }

数往知来 JQuery 缩略图_客户端文件下载<二十四>

标签:

原文地址:http://www.cnblogs.com/hetong/p/4796206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!