博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET HttpPost 上传文件图片到服务器
阅读量:6991 次
发布时间:2019-06-27

本文共 3016 字,大约阅读时间需要 10 分钟。

public class ImageData    {        public string imageFilePath { get; set; }        public string tempFilePath { get; set; }    }
ImageData data = new ImageData();        [HttpPost]        public ImageData UploadFile()        {            HttpContext context = HttpContext.Current;            if (context.Request.Files.Count == 0)//判断文件是否存在                return null;            String outFilePath = context.Server.MapPath("~/ImageFile/");            string time = System.DateTime.Now.ToFileTimeUtc().ToString() + "/";            System.IO.Directory.CreateDirectory(outFilePath + time);            string imageFilePath = outFilePath + time + context.Request.Files[0].FileName;            context.Request.Files[0].SaveAs(imageFilePath);            string thumbnailFilePath = outFilePath + time + "temp_" + context.Request.Files[0].FileName;            getImage(imageFilePath, 60, 2, thumbnailFilePath);            data.imageFilePath = "localhost:12380/ImageFile/" + time + context.Request.Files[0].FileName;            data.tempFilePath = "localhost:12380/ImageFile/" + time + "temp_" + context.Request.Files[0].FileName;            return data;        }                ///         /// 生成缩略图        ///         /// 原始图片文件        /// 质量压缩比        /// 收缩倍数        /// 输出文件名        /// 
成功返回true,失败则返回false
public static bool getImage(String sourceFile, long quality, int multiple, String outputFile) { try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width; float yWidth = sourceImage.Height; Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple)); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple); g.Dispose(); newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); return true; } catch { return false; } } /// /// 获取图片编码信息 /// private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }

 

转载地址:http://cmwvl.baihongyu.com/

你可能感兴趣的文章
国内不谈java
查看>>
比较Maven和Ant
查看>>
poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
查看>>
dup和dup2函数
查看>>
Js的原型和原型链理解
查看>>
未知题目
查看>>
在C#中??和?分别是什么意思?
查看>>
APP 开发,代码写的真烂
查看>>
适合0基础的web开发系列教程-html5新的表单元素
查看>>
Python 常用算法记录
查看>>
OC中的野指针(僵尸指针)
查看>>
SSM环境的搭建
查看>>
leetcode 196. Delete Duplicate Emails
查看>>
聚美第六天
查看>>
Q:java中的泛型数组
查看>>
[Android] adb 命令 dumpsys activity , 用来看 task 中的activity。 (uninstall virus)
查看>>
数据分析学习笔记(三)-NetworkX的使用
查看>>
rm 命令简要
查看>>
xadmin快速搭建后台管理系统
查看>>
MySQL 5.7 分区表性能下降的案例分析
查看>>