SWFUpload无刷新文件批量上传
一、首先将SWFUpload所有文件加入项目中,如图
二、将swfupload和handlers两个文件引入页面中三、将以下代码引用页面中
<script type="text/javascript">
var swfu;
window.onload = function () {
swfu = new SWFUpload({
// Backend Settings
upload_url: "/ashx/CutPhoto.ashx",
post_params: {
"ASPSESSID": "<%=Session.SessionID %>"
},
// File Upload Settings
file_size_limit: "2 MB",
file_types: "*.jpg;*.gif",
file_types_description: "JPG Images",
file_upload_limit: 0, // Zero means unlimited
// Event Handler Settings - these functions as defined in Handlers.js
// The handlers are not part of SWFUpload but are part of my website and control how
// my website reacts to the SWFUpload events.
swfupload_preload_handler: preLoad,
swfupload_load_failed_handler: loadFailed,
file_queue_error_handler: fileQueueError,
file_dialog_complete_handler: fileDialogComplete,
upload_progress_handler: uploadProgress,
upload_error_handler: uploadError,
upload_success_handler: Show,//这里修改了方法的定义。
upload_complete_handler: uploadComplete,
// Button settings
button_image_url: "/SWFUpload/images/XPButtonNoText_160x22.png",
button_placeholder_id: "spanButtonPlaceholder",
button_width: 160,
button_height: 22,
button_text: '<span class="button">选择上传图片 <span class="buttonSmall">(2 MB Max)</span></span>',
button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
button_text_top_padding: 1,
button_text_left_padding: 5,
// Flash Settings
flash_url: "/SWFUpload/swfupload.swf", // Relative to this file
flash9_url: "/SWFUpload/swfupload_FP9.swf", // Relative to this file
custom_settings: {
upload_target: "divFileProgressContainer"
},
// Debug Settings
debug: false
});
}
//上传成功以后执行该方法
function Show(file, serverData) {
var s = serverData.split(':');//接收从服务端返回的数据,按照分号分隔
if (s[0] == "ok") {
$("#imgSrc").attr("src", s[1]);
}
}
</script>
四、创建一般处理程序(代码如下)
if (context.Request["action"].ToString() == "up")
{
HttpPostedFile file = context.Request.Files["Filedata"]; //获取上传的文件数据
string fileName = Path.GetFileName(file.FileName);//获取上传文件的名称
string fileExt = Path.GetExtension(fileName); //得到文件的扩展名
if (fileExt == ".jpg")
{
//将上传的图片放到不同的文件夹下(根据日期)
string dir = "/UploadImage/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(dir))); //创建文件夹
//文件生命名字
string furllDir = dir + Common.WebCommon.GetStreamMD5(file.InputStream) + fileExt; //构建完整的文件路径
using (Image img = Image.FromStream(file.InputStream))
{ //根据上传图片创建一个Image,获取图片的高度与宽度
file.SaveAs(context.Server.MapPath(furllDir)); //把图片保存起来
context.Response.Write("ok:" + furllDir + ":" + img.Width + ":" + img.Height);//将图片路径与图片的高度与宽度返回浏览器
}
}
}
else if (context.Request["action"].ToString() == "cut") {
//接收参数
int x = Convert.ToInt32(context.Request.Form["x"]);
int y = Convert.ToInt32(context.Request.Form["y"]);
int width = Convert.ToInt32(context.Request.Form["width"]);
int height = Convert.ToInt32(context.Request.Form["height"]);
string imgSrc=context.Request.Form["imgSrc"];
//创建画布
using (Bitmap map=new Bitmap(width,height)) //将红色div确定范围到画布上
{
//画笔
using (Graphics g=Graphics.FromImage(map))
{
//用画笔将图片画到画布上
using(Image img=Image.FromFile(context.Server.MapPath(imgSrc))){
//1.指定的是对哪张图片进行操作
//2.指定画多么大
//3.画img的那一部分
g.DrawImage(img,new Rectangle(0,0,width,height),new Rectangle(x,y,width,height),GraphicsUnit.Pixel);
string newfileName = Guid.NewGuid().ToString().Substring(0,8);
map.Save(context.Server.MapPath("/UploadImage/")+newfileName+".jpg"); //保存截取后的图片
context.Response.Write("/UploadImage/"+newfileName+".jpg");
}
}
}
}