WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件。在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览器,沿用原来的FLASH运行时,兼容IE6+,iOS 6+, android 4+。两套运行时,同样的调用方式,可供用户任意选用。 采用大文件分片并发上传,极大的提高了文件上传效率。

1、引用脚本及样式

 <!--引入CSS-->
<link rel="stylesheet" type="text/css" href="webuploader文件夹/webuploader.css"> <!--引入JS-->
<script type="text/javascript" src="webuploader文件夹/webuploader.js"></script>
<!--引入JS-->
<script type="text/javascript" src="js2/jquery-1.10.2.min.js"></script>
<!--引入初始化JS及图片上传到文件服务器-->
<script type="text/javascript" src="js2/getting-starteder.js"></script>
getting-starteder.js--图片初始化及处理上传到服务器全是靠这个JS来实现
 // 图片上传demo
jQuery(function() {
var $ = jQuery,
$list = $('#fileList'),
// 优化retina, 在retina下这个值是2
ratio = window.devicePixelRatio || , // 缩略图大小
thumbnailWidth = * ratio,
thumbnailHeight = * ratio, // Web Uploader实例
uploader; // 初始化Web Uploader
uploader = WebUploader.create({ // 自动上传。
auto: true, // swf文件路径
swf: BASE_URL + '/js2/Uploader.swf', // 文件接收服务端。
// server: 'http://webuploader.duapp.com/server/fileupload.php',
server: 'Handler/UploadHandleringNew.ashx', // 选择文件的按钮。可选。
// 内部根据当前运行是创建,可能是input元素,也可能是flash.
pick: '#filePicker', // 只允许选择文件,可选。
accept: {
title: 'Images',
extensions: 'gif,jpg,jpeg,bmp,png',
mimeTypes: 'image/*'
}
}); // 当有文件添加进来的时候
uploader.on( 'fileQueued', function( file ) {
var $li = $(
'<div id="' + file.id + '" class="file-item thumbnail">' +
'<img>' +
'<div class="info">' + file.name + '</div>' +
'</div>'
),
$img = $li.find('img'); $list.append( $li ); // 创建缩略图
uploader.makeThumb( file, function( error, src ) {
if ( error ) {
$img.replaceWith('<span>不能预览</span>');
return;
} $img.attr( 'src', src );
}, thumbnailWidth, thumbnailHeight );
}); // 文件上传过程中创建进度条实时显示。
uploader.on( 'uploadProgress', function( file, percentage ) {
var $li = $( '#'+file.id ),
$percent = $li.find('.progress span'); // 避免重复创建
if ( !$percent.length ) {
$percent = $('<p class="progress"><span></span></p>')
.appendTo( $li )
.find('span');
} $percent.css( 'width', percentage * + '%' );
}); // 文件上传成功,给item添加成功class, 用样式标记上传成功。
uploader.on( 'uploadSuccess', function( file ) {
$( '#'+file.id ).addClass('upload-state-done');
}); // 文件上传失败,现实上传出错。
uploader.on( 'uploadError', function( file ) {
var $li = $( '#'+file.id ),
$error = $li.find('div.error'); // 避免重复创建
if ( !$error.length ) {
$error = $('<div class="error"></div>').appendTo( $li );
} $error.text('上传失败');
}); // 完成上传完了,成功或者失败,先删除进度条。
uploader.on( 'uploadComplete', function( file ) {
$('#' + file.id).find('.progress').remove();
//alert('上传成功');
});
});

2、页面上代码:

<!--dom结构部分-->
<div id="uploader-demo">
<!--用来存放item-->
<div id="fileList" class="uploader-list"></div>
<div id="filePicker">选择图片</div>
</div>

我用是NET+ashx

上传后,由于WebUploader后,缩略图的正常,而上传服务器后,横拍的图片显示

如下图:

--上传到服务器后

解决方案:

在上传到服务器之前对图片进行强制转换方向,服务端把 jpeg 的图片纠正一下

根据照片的Exif信息修正

 public static void RotateImage(Image img)
{
PropertyItem[] exif = img.PropertyItems;
byte orientation = ;
foreach (PropertyItem i in exif)
{
if (i.Id == )
{
orientation = i.Value[];
i.Value[] = ;
img.SetPropertyItem(i);
}
} switch (orientation)
{
case :
img.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case :
img.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case :
img.RotateFlip(RotateFlipType.RotateNoneFlipY);
break;
case :
img.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case :
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case :
img.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case :
img.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
default:
break;
}
foreach (PropertyItem i in exif)
{
if (i.Id == )
{
i.Value = BitConverter.GetBytes(img.Width);
}
else if (i.Id == )
{
i.Value = BitConverter.GetBytes(img.Height);
}
}
}

这样就可以完美解决!!!

Exif specification 对图片方向说明

关于Exif Orientation标志的定义 http://sylvana.net/jpegcrop/exif_orientation.html

UploadHandleringNew.ashx

Value 0th Row 0th Column
1 top left side
2 top right side
3 bottom right side
4 bottom left side
5 left side top
6 right side top
7 right side bottom
8 left side bottom
 <%@ WebHandler Language="C#" Class="UploadHandleringNew" %>

 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ysw.BLL;
using ysw.Models;
using System.Drawing; public class UploadHandleringNew : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public string camera = String.Empty;//机型
public string focalLength = String.Empty;//焦距
public string shutterSpeed = String.Empty;//速度
public string aperture = String.Empty;//光圈
public string iso = String.Empty;//感光度
public string treePath = System.Web.HttpContext.Current.Server.MapPath("~/");
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Collections.Generic.IList<TempImages> list = new System.Collections.Generic.List<TempImages>();
if (context.Session["TempImageList"] != null)
{
list = context.Session["TempImageList"] as System.Collections.Generic.IList<TempImages>;
} if (list.Count >= )
{
context.Response.Write("上传数量已经超出系统限制的6个文件");
return; }
else
{
HttpPostedFile file = context.Request.Files[];
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\"; if (file != null)
{
if (!System.IO.Directory.Exists(uploadPath))
{
System.IO.Directory.CreateDirectory(uploadPath);
}
Random random = new Random();
string fileName = DateTime.Now.ToString("yyyy-MM-dd") + "-" + random.Next() + file.FileName.Substring(file.FileName.Length - ); // 文件名称
string fileName_s = "x_" + fileName; // 缩略图文件名称
string fileName_syp = "water_" + fileName; // 水印图文件名称(图片)
string webFilePath = "";
string webFilePath_s = "";
string webFilePath_s1 = "";
string webFilePath_syp = "";
webFilePath = treePath + @"originalImages\" + fileName; // 服务器端文件路径
webFilePath_s = treePath + @"thumbnails\" + fileName_s; // 服务器端缩略图路径
webFilePath_syp = treePath + @"waterImages\" + fileName_syp; // 服务器端带水印图路径(图片)
webFilePath_s1 = treePath + @"thumbnails166\" + fileName_s; // 服务器端缩略图路径
string webFilePath_sypf = System.Web.HttpContext.Current.Server.MapPath(@"~/images/synew.png");  // 服务器端水印图路径(图片)
file.SaveAs(webFilePath); AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);//生成图片水印图 int towidth = ;
int toheight = ;
//MakeImage1(webFilePath, webFilePath_syp, 500);
MakeImage(webFilePath, webFilePath_s, );
//将临时数据保存到临时表中
TempImages tempImage = new TempImages();
tempImage.ISBN = fileName;
if (context.Session["UserInfo"] != null)
{
tempImage.UserId = (context.Session["UserInfo"] as ysw.Model.UserInfo).Id;
}
else
{
tempImage.UserId = ;
} tempImage.CreateTime = DateTime.Now;
tempImage.Id = TempImageManager.AddTempImages(tempImage);
if (tempImage.Id > )
{
list.Add(tempImage);
context.Session["TempImageList"] = list;
string str = "waterImages/" + fileName_syp + ",thumbnails/" + fileName_s + "," + camera + "," + focalLength + "," + shutterSpeed + "," + aperture + "," + iso;
//上传成功后让上传队列的显示自动消失
//string str = fileName + ",";
//int toheight1 = 90;
// int towidth1 = 90;
// if ((double)towidth >(double)toheight)
// {
// toheight1 = toheight * 90 / towidth;
// }
// else
// {
// towidth1 = towidth * 90 / toheight;
// }
// str += towidth1 + "," + toheight1;
context.Response.Write(str);
}
else
{
context.Response.Write("");
}
}
else
{
context.Response.Write("");
}
}
}
public static void rotating(string Path_syp)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Path_syp); System.Drawing.Imaging.PropertyItem[] exif = img.PropertyItems;
byte orientation = ;
foreach (System.Drawing.Imaging.PropertyItem i in exif)
{
if (i.Id == )
{
orientation = i.Value[];
i.Value[] = ;
img.SetPropertyItem(i);
}
} switch (orientation)
{
case :
img.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case :
img.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case :
img.RotateFlip(RotateFlipType.RotateNoneFlipY);
break;
case :
img.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case :
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case :
img.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case :
img.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
default:
break; }
foreach (System.Drawing.Imaging.PropertyItem i in exif)
{
if (i.Id == )
{
i.Value = BitConverter.GetBytes(img.Width);
}
else if (i.Id == )
{
i.Value = BitConverter.GetBytes(img.Height);
}
} //System.Drawing.Image bitmap = new System.Drawing.Bitmap(500, 500); //System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); // //以jpg格式保存缩略图
// bitmap.Save(Path_syp, System.Drawing.Imaging.ImageFormat.Jpeg); // //image.Dispose();
// bitmap.Dispose();
// g.Dispose(); }
/// <summary>
/// 生成缩略图方法(700px图片)
/// </summary>
/// <param name="webFilePath"></param>
/// <param name="webFilePath_s"></param>
/// <param name="size"></param>
private static void MakeImage1(string webFilePath, string webFilePath_s, int size)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(webFilePath);
int towidth = size;
int toheight = size;
int x = ;
int y = ;
int ow = image.Width;
int oh = image.Height;
toheight = image.Height * size / image.Width;
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(image, new System.Drawing.Rectangle(, , towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
bitmap.Save(webFilePath_s, System.Drawing.Imaging.ImageFormat.Jpeg);
image.Dispose();
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="webFilePath">原图的路径</param>
/// <param name="webFilePath_s">缩略图的路径</param>
/// <param name="size">缩略图的大小</param>
public static void MakeImage(string webFilePath, string webFilePath_s, int size)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(webFilePath);
int towidth = size;
int toheight = size;
int x = ;
int y = ;
int ow = image.Width;
int oh = image.Height; if ((double)image.Width / (double)image.Height > (double)towidth / (double)toheight)
{
oh = image.Height;
ow = image.Height * towidth / toheight;
y = ;
x = ;
}
else
{
ow = image.Width;
oh = image.Width * toheight / towidth;
x = ;
y = ;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(image, new System.Drawing.Rectangle(, , towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(webFilePath_s, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
image.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
/// <summary>
/// 生成水印缩略图
/// </summary>
/// <param name="Path"></param>
/// <param name="Path_syp"></param>
/// <param name="Path_sypf"></param>
protected void AddWaterPic(string Path, string Path_syp, string Path_sypf)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); System.Drawing.Imaging.PropertyItem[] exif = image.PropertyItems;
byte orientation = ;
foreach (System.Drawing.Imaging.PropertyItem i in exif)
{
if (i.Id == )
{
orientation = i.Value[];
i.Value[] = ;
image.SetPropertyItem(i);
}
} switch (orientation)
{
case :
image.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case :
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case :
image.RotateFlip(RotateFlipType.RotateNoneFlipY);
break;
case :
image.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case :
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case :
image.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case :
image.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
default:
break; }
foreach (System.Drawing.Imaging.PropertyItem i in exif)
{
if (i.Id == )
{
i.Value = BitConverter.GetBytes(image.Width);
}
else if (i.Id == )
{
i.Value = BitConverter.GetBytes(image.Height);
}
} int towidth = ;
int toheight = ;
int x = ;
int y = ;
int ow = image.Width;
int oh = image.Height;
if (image.Width < )
{
toheight = image.Height;
towidth = image.Width;
}
else
{
towidth = ;
toheight = image.Height * / image.Width; }
//rotating(Path); System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(image, new System.Drawing.Rectangle(, , towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); g.DrawImage(copyImage, new System.Drawing.Rectangle(bitmap.Width - - copyImage.Width, bitmap.Height - - copyImage.Height, copyImage.Width, copyImage.Height), , , copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose(); bitmap.Save(Path_syp);
image.Dispose();
}
public bool IsReusable {
get {
return false;
}
} }

使用WebUploader使用,及使用后测试横拍或竖拍图片图片方向不对等解决方案的更多相关文章

  1. 打完补丁后测试db_link对SCN的影响

    环境:11.2.0.4.0 升 11.2.0.4.8 后测试 背景:oracle 的db_link会导致实例间SCN同步,SCN增长速度过快则会产生错误: 方案:oracle官方推荐升级版本,但升级之 ...

  2. iOS 测试在应用发布前后的痛点探索以及解决方案

    作者-芈 峮 前言 iOS 开发从 2010 年开始在国内不断地升温,开发和测试相关的问题不绝于耳.iOS 测试主要涉及哪些内容?又有哪些挑战呢?带着疑问我们开始第一个大问题的讨论. iOS 测试的范 ...

  3. 采用 ITextPDF 类库测试向 PDF 中加入图片的示例

    package com.smbea.image; import com.artup.util.image.ImageUtil; import com.itextpdf.text.*; import c ...

  4. 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案

    更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...

  5. android单元测试 activity跳转 以及 input 输入后 测试

    Android junit实现多个Activity跳转测试 分类: Android Junit测试2011-11-14 16:49 1601人阅读 评论(2) 收藏 举报 androidjunitla ...

  6. Net Manager测试连接测试没有成功,用户权限问题,以管理员身份运行后测试成功

    Net Manager测试连接测试没有成功,截图如下:

  7. xcode XCTest录制后测试出错临时解决方法

    Xcode 使用 XCTest 做自动化测试,录制后在run UITest 的时候总是莫名报错,后来在方法执行前添加sleep()后,没有再出现错误,可能是xcode的一处BUG.

  8. 使用Jmeter3.1进行接口测试(包含需登录后测试的接口)

    Jmeter版本为3.1,以下只针对此版本进行测试说明: 1.打开Jmeter3.1: 启动命令路径:apache-jmeter-3.1\bin\jmeter.bat 2.测试步骤: 1.测试计划-- ...

  9. rails中migration数据库后测试不通过的问题

    rails项目中由于后期需求变化,需要在products数据库中增加一个字段来满足多国家商品的分类: rails g migration add_locale_to_products locale:s ...

随机推荐

  1. Yukari's Birthday

    hdu4430:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题意:题目的意思就是给你一个s,让你求k,r,其中k,r,满足:k^1+k^2+..... ...

  2. jQuery Validate 验证,校验规则写在控件中的具体例子

    将校验规则写到控件中 <script src="../js/jquery.js" type="text/javascript"></scrip ...

  3. find the most comfortable road

    XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Spee ...

  4. 「Poetize7」电话线路

    描述 每台电话都有一个独一无二的号码,用一个十位的十进制数字串表示.电话a和b之间能直接通信,当且仅当“a与b之间仅有一个数字不同”,或者“交换a的某 两位上的数字后,a与b相同”.而a.b之间建立通 ...

  5. Linux企业级开发技术(4)——epoll企业级开发之epoll例程

    为了使大家更加深入了解epoll模型在企业应用中的使用,下面给出一段基于epoll的服务器代码,并在代码中添加了详细注释: #include <deque> #include <ma ...

  6. 【转】爱普生打印机L358

    原文网址:http://www.chiphell.com/thread-838403-1-1.html 儿子幼儿园老是要打印作业,就决定用他的压岁钱买个打印机,主要以文件打印为主,兼顾照片.昨天网上一 ...

  7. wpa_cli与wpa_supplicant的交互命令

    1)通过adb命令行,可以直接打开supplicant,从而运行wpa_cli,可以解决客户没有显示屏而无法操作WIFI的问题,还可以避免UI的问题带到driver.进一步来说,可以用在很多没有键盘输 ...

  8. 数据结构(Splay平衡树): [NOI2007] 项链工厂

    [NOI2007] 项链工厂 ★★★   输入文件:necklace.in   输出文件:necklace.out   简单对比 时间限制:4 s   内存限制:512 MB [问题描述] T公司是一 ...

  9. VS2010如何生成release文件

    点击生成-->配置管理器-->活动解决方案配置下拉菜单中选择release就行了,最后再编译一下就在相应的目录下生成了

  10. [Locked] Count Univalue Subtrees

    Count Univalue Subtrees Given a binary tree, count the number of uni-value subtrees. A Uni-value sub ...