js

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js上传图片</title>
</head>
<body>
<input id="file" type="file"> <script type="text/javascript">
window.onload = function myfunction() {
var input = document.getElementById('file');
input.onchange = function () {
var files = this.files;
for (var i = 0, len = files.length; i < len; i++) {
var file = files[i];
//file.name || file.fileName => 文件名称
//file.size || file.fileSize => 文件大小
upload_html5(file);
}
};
} function upload_html5(file) {
//html5 上传
var xhr = new XMLHttpRequest();
//上传进度事件
xhr.upload.addEventListener("progress", function (e) { }, false);
//上传完成(成功)事件
xhr.addEventListener("load", function (e) {
//获取服务器响应
var obj = JSON.parse(e.target.responseText);
if (obj.code == 200) {
alert("上传成功");
}
else {
alert(obj.message);
}
}, false);
//上传失败事件
xhr.addEventListener("error", function (e) {
console.log(e);
alert("上传失败");
}, false);
//上传中断(取消)事件
xhr.addEventListener("abort", function (e) {
alert("上传取消");
}, false); var fd = new FormData;
//添加要上传的文件对象
fd.append("file", file);
fd.append("hallName", '分会场1');
fd.append("dateTime", '2019-06-21');
xhr.open("POST", "/Common/UploadFile");
xhr.send(fd);
}
</script>
</body>
</html>

C#

        public ActionResult UploadFile()
{
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var directory = string.Format("{0}/Upload/{1}/", baseDirectory, Request.Form["hallName"]);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory); var file = Request.Files[];
var fileTypes = new string[] { "image/jpeg", "image/gif", "image/png" };
if (!fileTypes.Contains(file.ContentType))
return Json(new Response(, "只可上传图片")); Image img = Image.FromStream(file.InputStream);
var filePath = string.Format("{0}/{1}.jpg", directory, Request.Form["dateTime"]);
ImageCompress(img, filePath, );
return Json(new Response());
}
/// <summary>
/// 将图片按百分比压缩
/// </summary>
public static bool ImageCompress(Image source, string savePath, byte percent)
{
var parameters = new EncoderParameters();
parameters.Param[] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { percent });
try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo jpegICIinfo = arrayICI.SingleOrDefault(a => a.FormatDescription.Equals("JPEG", StringComparison.OrdinalIgnoreCase));
if (jpegICIinfo != null)
source.Save(savePath, jpegICIinfo, parameters);
else
source.Save(savePath, source.RawFormat);
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
source.Dispose();
}
}

最新文章

  1. Linux学习之五--常用操作
  2. xamarin UWP中MessageDialog与ContentDialog的区别
  3. WebSocket 学习笔记--IE,IOS,Android等设备的兼容性问题与代码实现
  4. 【HTML5】video视频
  5. Trie implementation
  6. C# DataRow[]转换DataTable
  7. Python获取区域面积
  8. [转]-nohup-真正的Shell后台运行
  9. Lintcode470-Tweaked Identical Binary Tree-Easy
  10. Java:编码与乱码问题
  11. 细说MySQL数据库操作
  12. Sqlserver 连接oracle和mysql数据库 已经oracle导入sqlserver表数据
  13. HTML5 data属性
  14. 数据中心网络(1)-VXLAN
  15. 四、Mosquitto 高级应用之用户配置
  16. chromium对网页获取favicon
  17. 【期望DP】BZOJ3450- Tyvj1952 Easy
  18. 从头学习MVC4基础之视图
  19. java 多线程超详细总结——阿里大牛熬夜整理
  20. [SRM568]DisjointSemicircles

热门文章

  1. gdb core调试
  2. robot设置chrome mobile emulation
  3. 使用 SQL Server Management Studio的活动和监视器 查看运行的SQL语句
  4. python 面向对象八 多继承
  5. 开源基于asio的网络通信框架asio2,支持TCP,UDP,HTTP,RPC,SSL,跨平台,支持可靠UDP,支持TCP自动拆包,TCP数据报模式等
  6. 原生JavaScript之实战 模拟重力场(篮球)
  7. Taro 小程序 自定义导航栏
  8. Hdu 5379 Mahjong tree (dfs + 组合数)
  9. 转: ORA-06508 could not find program unit being called: &quot;DBSNMP.BSLN_INTERNAL
  10. 一个简单的jsp+servlet登录界面的总结