zoukankan      html  css  js  c++  java
  • .NET HttpPost 上传文件图片到服务器

     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;
            }
    
            
            /// <summary>
            /// 生成缩略图
            /// </summary>
            /// <param name="sourceFile">原始图片文件</param>
            /// <param name="quality">质量压缩比</param>
            /// <param name="multiple">收缩倍数</param>
            /// <param name="outputFile">输出文件名</param>
            /// <returns>成功返回true,失败则返回false</returns>
            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;
                }
            }
           
    
           
            /// <summary>
            /// 获取图片编码信息
            /// </summary>
            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;
            }
  • 相关阅读:
    Python全栈开发记录_第七篇(模块_time_datetime_random_os_sys_hashlib_logging_configparser_re)
    Python全栈开发记录_第六篇(生成器和迭代器)
    Python全栈开发记录_第五篇(装饰器)
    Python全栈开发记录_第四篇(集合、函数等知识点)
    Python全栈开发记录_第三篇(linux(ubuntu)的操作)
    Python全栈开发记录_第二篇(文件操作及三级菜单栏增删改查)
    Python全栈开发记录_第一篇(循环练习及杂碎的知识点)
    NET控件Designer架构设计
    如何把Excel中的单元格等对象保存成图片
    “某某云词典” – 纠结的初体验
  • 原文地址:https://www.cnblogs.com/Warmsunshine/p/9853708.html
Copyright © 2011-2022 走看看