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 == )//判断文件是否存在
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[].FileName;
context.Request.Files[].SaveAs(imageFilePath);
string thumbnailFilePath = outFilePath + time + "temp_" + context.Request.Files[].FileName;
getImage(imageFilePath, , , thumbnailFilePath); data.imageFilePath = "localhost:12380/ImageFile/" + time + context.Request.Files[].FileName;
data.tempFilePath = "localhost:12380/ImageFile/" + time + "temp_" + context.Request.Files[].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();
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality);
myEncoderParameters.Param[] = 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, , , 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 = ; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
} return null;
}

.NET HttpPost 上传文件图片到服务器的更多相关文章

  1. 在C#客户端用HTTP上传文件到Java服务器

    在C#客户端用HTTP上传文件到Java服务器  来源:http://www.cnblogs.com/AndyDai/p/5135294.html 最近在做C / S 开发,需要在C#客户端上传文件到 ...

  2. SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案

    SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOEx ...

  3. C# 上传文件至远程服务器

    C# 上传文件至远程服务器(适用于桌面程序及web程序) 2009-12-30 19:21:28|  分类: C#|举报|字号 订阅     最近几天在玩桌面程序,在这里跟大家共享下如何将本地文件上传 ...

  4. ASP.NET上传文件到远程服务器(HttpWebRequest)

    /// <summary> /// 文件上传至远程服务器 /// </summary> /// <param name="url">远程服务地址 ...

  5. asp.net 服务器 上传文件到 FTP服务器

    private string ftpServerIP = "服务器ip";//服务器ip private string ftpUserID = "ftp的用户名" ...

  6. .Net 上传文件到ftp服务器和下载文件

    突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...

  7. java 上传文件到 ftp 服务器

    1.  java 上传文件到 ftp 服务器 package com.taotao.common.utils; import java.io.File; import java.io.FileInpu ...

  8. PHP 上传文件到其他服务器

    PHP 上传文件到其他服务器 标签(空格分隔): 安装Guzzle类库 **guzzle** 是发送网络请求的类库 composer安装:**composer require guzzlehttp/g ...

  9. SpringBoot上传文件到本服务器 目录与jar包同级问题

    目录 前言 原因 实现 不要忘记 最后的封装 Follow up   前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了.当你使用tomcat发布项目的时候,上传 ...

随机推荐

  1. 6. 合并排序数组 II

    6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...

  2. C#常用字符串函数

    Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 CompareOrdinal 与Compare一样,但不考虑文化背景 Format 格式化包含各种值的字符串和如何格式化每 ...

  3. C - Rikka with Badminton --- HDU 6425 快速幂加组合数学

    ---恢复内容开始--- 题目链接: https://vjudge.net/problem/1812693/origin 这题的mod运算很恶心,真的... 本题有正反两个思路,一个是正面求解其不能成 ...

  4. RabbitMQ主题模式

    Send类 package topics; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; imp ...

  5. Git——常用场景解析

    总结:本篇文章从初识GitHub.Git,实践GitHub的五种常用场景,分别是:git for windows安装,git配置,克隆远程代码到本地,上传本地代码到远程以及Git的常用指令.相信Jam ...

  6. [CC-CMPVIRS]Computer Virus

    [CC-CMPVIRS]Computer Virus 题目大意: 有一张纸带,从左到右被分成了\(n(n\le10^7)\)个格子,在刚开始,第\(i\)个格子上写着数字\(i\).这张纸带被分成了从 ...

  7. select超全超详细总结篇

    1.经常使用到select,每次都是翻翻翻资料,干脆总结一下,方便日后查看.   2.checked     匹配所有选中的被选中元素(复选框.单选框等,不包括select中的option)   se ...

  8. HTML 标题标签

    HTML:超文本标记语言基本结构 <!DOCTYPE html> <!--文档的声明 一个HTML文件就是一个文档 --> <html lang="en&quo ...

  9. Flask路由

    @app.route() methods:当前url地址,允许访问的请求方式 @app.route("/info", methods=["GET", " ...

  10. PHP01

    1.服务端开发基础 前端开发最终还是属于web端开发的一个分支,想要成为前端程序员,就要充分理解web的概念. 网站的概念: 可以在浏览器上通过一个地址直接访问使用 用于提供一种或多种特定服务的一系列 ...