Uploadify 3.2 上传图片
uploadify version: uploadify 3.2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var site = "http://pic.domain.com/";
</script>
<link href="css/common.css" rel="stylesheet" type="text/css" />
<link href="js/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.insert-pool li{list-style:none; width:100px;height:100px;margin-right:20px;}
</style>
<script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="js/uploadify/jquery.uploadify.min.js" type="text/javascript"></script> </head>
<body>
<form id="form1" runat="server">
<div id="page">
<div id="content">
<div id="upload-content">
<div class="upload-form">
<dl>
<dt>上传图片:</dt>
<dd class="uploader"><div id="file_upload"></div></dd>
</dl>
</div>
</div>
<div class="insert-pool"></div>
</div>
</div>
</form>
</body>
</html>
JavaScript:
<script type="text/javascript">
function imgResize(maxWidth, maxHeight, imgObj) {
var img = new Image();
img.src = imgObj.src; if (img.width > 0 && img.height > 0) {
if (img.width / img.height >= maxWidth / maxHeight) {
if (img.width > maxWidth) {
imgObj.width = maxWidth;
imgObj.height = (img.height * maxWidth) / img.width;
} else {
imgObj.width = img.width;
imgObj.height = img.height;
}
} else {
if (img.height > maxHeight) {
imgObj.height = maxHeight;
imgObj.width = (img.width * maxHeight) / img.height;
} else {
imgObj.width = img.width;
imgObj.height = img.height;
}
}
}
else {
imgObj.width = maxWidth;
imgObj.height = maxHeight;
}
} $(function () {
//上传图片
$("#file_upload").uploadify({
'fileSizeLimit': '2048KB',
'fileTypeExts': '*.gif; *.jpg',
'queueSizeLimit': 3,
'auto': true,
'swf': '/js/uploadify/uploadify.swf',
'uploader': '/Handler/ImgUploadHandler.ashx',
'buttonText': '选择图片并上传',
'onUploadSuccess': function (file, data, response) {
if (data && data.length > 0) {
var json = eval("(" + data + ")");
$("<li><div class=\"img-box\"><img src=\"" + site + json.url + "\" onload=\"imgResize(100,100,this);\" /></div><a href=\"javascript:;\" class=\"remove\" title=\"是否移除图片\">移除</a></li>")
.appendTo(".insert-pool");
} else {
alert("文件上传数据为空");
}
}
}); $(".remove").live("click", function () {
var oThis = $(this);
var imgsrc = oThis.prev().find("img").attr("src");
if (imgsrc.lastIndexOf("/") > -1) {
imgsrc = imgsrc.substring(imgsrc.lastIndexOf("/") + 1);
//alert(imgsrc);
}
$.post("/Handler/ImgUploadHandler.ashx", { "action": "IMGDELETE", "imgsrc": imgsrc },
function (data) {
if (data && data != "") {
var json = eval("(" + data + ")");
if (json.state == "SUCCESS") {
oThis.parent("li").eq(0).remove();
} else {
alert("移除失败");
}
}
});
});
});
</script>
Code:
public class ImgUploadHandler : IHttpHandler
{ string strMsg = "SUCCESS", action = "";
string rootpath = null,fileName = null, filePath = null; public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"; HttpPostedFile file = context.Request.Files["Filedata"];
action = context.Request["action"];
rootpath = System.Configuration.ConfigurationManager.AppSettings["PictureRoot"]; if (action == "IMGDELETE")
{
ImgDelete(context);
} if (file != null)
{
string fileExt = System.IO.Path.GetExtension(file.FileName); if (!System.IO.Directory.Exists(rootpath))
{
System.IO.Directory.CreateDirectory(rootpath);
}
fileName = System.DateTime.Now.ToString("yyyyMMddhhmmssffffff") + fileExt;
filePath = rootpath + fileName;
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(filePath);
} file.SaveAs(filePath);
if (filePath.LastIndexOf("\\") > -)
filePath = filePath.Substring(filePath.LastIndexOf("\\") + );
context.Response.Write("{\"state\":\"" + strMsg + "\", \"url\":\"" + filePath + "\"}");
}
else
{
strMsg = "FAILED";
context.Response.Write("{\"state\":\"" + strMsg + "\", \"url\":\"" + filePath + "\"}");
}
} private void ImgDelete(HttpContext context)
{
fileName = context.Request["imgsrc"];
filePath = rootpath + fileName;
if(System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
context.Response.Write("{\"state\":\"" + strMsg + "\"}");
}
else
{
strMsg = "FAILED";
context.Response.Write("{\"state\":\"" + strMsg + "\"}");
}
context.Response.End();
} public bool IsReusable
{
get
{
return false;
}
}
}
Uploadify 3.2 上传图片的更多相关文章
- Uploadify 控件上传图片 + 预览
jquery的Uploadify控件上传图片和预览使用介绍. 在简单的servlet系统中和在SSH框架中,后台处理不同的,在三大框架中图片预览时费了不少力气,所以下面将两种情况都介绍一下. 1,前台 ...
- 【Uploadify】远程上传图片到【七牛云存储】
1.下载Uploadify版本3.2.1 2.下载七牛SDK 解压后将 qiniu 文件夹copy到uploadify文件夹下 3.修改uploadify.php文件 <?php $verify ...
- asp.net+uploadify实现图片上传图片
前段代码如下 $("#file_upload").uploadify({ 'auto': true, 'swf': '/template/js/cutImg/uploadify/u ...
- uploadify上传图片的类型错误的解决办法
大家在做开发的过程中,相信很多人都会使用到uploadify插件来上传图片,但是这个插件也有不完美的地方. 我曾多次遇到过这样一个问题:上传的图片类型明明是没有问题的,但是在上传的时候总是会报错:图片 ...
- jquery uploadify修改上传的文件名和显示
如果觉得看文章太麻烦,可以直接看参考:http://stackoverflow.com/questions/7707687/jquery-uploadify-change-file-name-as-i ...
- Uploadify使用源码
上传图片页面绑定源码如下: $("#uploadify").uploadify({ 'uploader' : basePath+'commons/uploadfiles/uploa ...
- 单文件WebUploader做大文件的分块和断点续传
前言: WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流 ...
- 调试台自动多出现一个'' ,我 用uploadify上传图片时,在给页面写入一个返回值为图片名称的变量的值的时候值的前面始终多出现一个''
对你有助请点赞,请顶,不好请踩------送人玫瑰,手留余香! 15:54 2016/3/12用uploadify上传图片时,在给页面写入一个返回值为图片名称的变量的值的时候值的前面始终多出现一个' ...
- MVC中使用jquery uploadify上传图片报302错误
使用jquery uploadify上传图片报302错误研究了半天,发现我上传的action中有根据session判断用户是否登录,如果没有登录就跳到登陆页,所以就出现了302跳转错误.原来更新了fl ...
随机推荐
- HTML超出文本显示省略号...[text-overflow]
需要对div或者span同时应用Css: text-overflow:ellipsis; white-space:nowrap; overflow:hidden; 即可实现所想要得到的溢出文本显示省略 ...
- 自定义的UITabbar上面的按钮的x坐标的计算方法
; i < 4; i++) {//4是按钮的个数 NSString *backImage = backgroud[i]; NSString *heightImage = heightBackgr ...
- 什么是SMART原则?
SMART(S=Specific.M=Measurable.A=Attainable.R=Relevant.T=Time-bound)原则即目标管理,目标管理是使经理的工作变被动为主动的一个很好的手段 ...
- sublime text配置
安装Package Control: 按”Ctrl”+”`”,在输入框中输入以下代码 Sublime Text3: import urllib.request,os,hashlib; h = '291 ...
- SourceTree基本操作
下载地址:https://www.sourcetreeapp.com 1.从克隆远程仓库 2.填写git地址 3.克隆成功后会来点如下界面,点击testGitHub 4.scourceTree管理界面 ...
- Javascript之日历
<meta http-equiv="content-type" content="text/html; charset=utf-8"> <ti ...
- Objective-C设计模式——原型Prototype(对象创建)
1.原型 原型设计模式所谓原型设计模式,其实就是对象复制,这个特性在所有语言基本上都是存在的. 我们知道在OC中,对象赋值其实是对对象的引用复制,其实就是相当于C语言中的指针.创建了一个新的变量,但是 ...
- Ant打jar包指定MainClass
一般用ant打jar的时候不用指定程序的入口!这个jar一般是给其他app引用的. 但是如果该jar就是程序的启动jar.例如: java -jar abc.jar 这个时候需要指定jar的入口类! ...
- python+selenium环境配置(windows7环境)
下载python[python开发环境] http://python.org/getit/ 下载setuptools[python的基础包工具] http://pypi.python.org/pypi ...
- ubuntu设置关闭屏幕和锁定
见链接:http://askubuntu.com/questions/177348/how-do-i-disable-the-screensaver-lock If you want to wrap ...