前台代码:<script type="text/javascript">
$(document).ready(function()
{
$("#uploadify").uploadify({
'uploader': '../../js/jquery.uploadify-v2.1.0/uploadify.swf',
'script': '../../UploadHandler.ashx',
'scriptData': {'filepath':'<%= filepath %>'},
'cancelImg': '../../js/jquery.uploadify-v2.1.0/cancel.png',
//'folder': '../../pic/'+<%= filepath %>,
'folder': '../../pic',
'queueID': 'fileQueue',
'auto': false,
'multi': false,
//'buttonImg':'../../pic/shangchuan.jpg',
'width':'90',//设置按钮离的左边距
// 'height':'80',//设置按钮离的上边距
'fileExt': '*.gif;*.jpg;*.jpeg;*.png;*.bmp',
'fileDesc': 'Image Files',
'method': 'GET',
'wmode':'transparent',
'buttonText':'浏览图片',
//'rollover':true
'onComplete':function(event,queueId,fileObj,response,data){
$('#filerigth').append('<img src="../../pic/'+response+'" alt="首饰" height="250" width="300">');
$('#Button1').hide();
$('#Label1').hide();
$('#Label2').hide();
}
});
});
后台UploadHandler.ashx
public class UploadHandler : IHttpHandler {
public readonly string connectionString = Crab_DBUtility.SqlHelper.LocalSqlServer;
protected string imagePath;//图片路径
protected string imageType;//图片类型
protected string imageName;//图片名称
protected string fileName;//图片名称
//提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行
//如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true;否则返回 false
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpPostedFile UploadImage = context.Request.Files["FileData"];
string filepath = context.Request.Params["filepath"];
//物理路径
string uploadpath = HttpContext.Current.Server.MapPath(context.Request["folder"] + "\\");
if (UploadImage != null)
{
string filename = UploadImage.FileName;
fileName = filepath + ".png";
}
else
{
context.Response.Write("0");
}
string mPath;
//取得图片名称
imagePath = UploadImage.FileName;
//取得图片类型
imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
imageName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);
Stream imgStream = UploadImage.InputStream;//流文件,准备读取上载文件的内容
int imgLen = UploadImage.ContentLength;//上载文件大小
//建立虚拟路径
mPath = HttpContext.Current.Server.MapPath(context.Request["folder"]);
//保存到虚拟路径
UploadImage.SaveAs(mPath + "\\" + fileName);
context.Response.Write(fileName);
}
public bool IsReusable
{
get
{
return false;
}
}
}
另外使用中文按钮的问题请参考carekee的博文
http://www.cnblogs.com/carekee/archive/2011/11/02/2233288.html
使用buttonImg是有来设置图片按钮,显示按钮的过程会很慢,不推荐
直接下载博主提供的
修复中文按钮乱码的jQuery uploadify (361)
覆盖即可,感谢博主。