Uploadify Version 3.2 上传
Uploadify Version 3.2 参数下载:http://download.csdn.net/detail/chen_657164/5014705
页面代码:
<script>
//上传图片
$(document).ready(function(){
upFile("#fileatts_fl");
});
function upFile(id){
$(id).uploadify({
'swf': 'uploadify/uploadify.swf',//上传flash按钮
'uploader': 'HandUpFile.ashx?id=2',//请求页面
'cancelImg': 'uploadify/uploadify-cancel.png',
'folder': 'attached',//上传文件目录
'queueID': 'divatts',//id
'auto': true,
'multi': false,
width :74,
height:23,
buttonText: '浏览...',//上传按钮文字
buttonImage :"/uploadify/20130417101359.jpg",//上传按钮路径
onUploadSuccess : function (file,data,response) {
//上传完成时触发(每个文件触发一次)
if (data == "error") {
alert("上传文件失败");
return;
}
else if(data == "errorSize"){
alert("上传文大小,不能超出2M,请核实!");
return;
}
else if(data =="errorType"){
alert("上传文件格式不正确,如:(.gif,.jpg,.jpeg)!");
return;
}
$("#divatts").append("<div id='" + data.split('.')[0] + "'> <input type='checkbox' name='txtatts' value='" + data +
"' checked='checked' onclick='this.checked=\"checked\";'/> " + data + "
<a href='javascript:' onclick='delatts(\"" + data + "\");'>删除附件</a></div>");
}
});
}
function delatts(file){
$.get("HandUpFile.ashx",{delfile:file},function(data){
if(data=="delok"){
alert("删除附件成功");
$("#"+file.split('.')[0]).remove();
}
return false;
});
}
</script>
HandUpFile.ashx 代码:
//接收要删除的文件名
string delfile = context.Request["delfile"];
string id = context.Request["id"];
if (!string.IsNullOrEmpty(delfile))
{
System.IO.File.Delete(context.Server.MapPath("/attached/thumbnail/") + delfile);
context.Response.Write("delok");
return;
}
if (id == "2") {
try
{
//获取要保存的文件夹名称
string path = context.Request["folder"];
//获取上传的文件对象
string file = context.Request["Filename"];
//获取客户上传控件
HttpPostedFile hf = context.Request.Files[0];
double size = hf.ContentLength / 1024;
if (size > 2048)
{
context.Response.Write("errorSize");
return;
}
string dPath = context.Request.MapPath("~/") + path + @"\";
if (!Directory.Exists(dPath))
{
Directory.CreateDirectory(dPath);
}
string fileType = file.Substring(file.LastIndexOf("."));
if (IsAllowedExtension(fileType))
{
string filename = DateTime.Now.Ticks + file.Substring(file.LastIndexOf("."));
//保存文件
string thumbfile = context.Server.MapPath(path) + "/attached/thumbnail/" + filename;
hf.SaveAs(thumbfile);
//向客户端输出刚刚保存的文件名
context.Response.Write(filename);
}
else
{
context.Response.Write("errorType");
return;
}
}
catch (Exception ex)
{
context.Response.Write("error");
}
}
/// <summary>
/// 上传格式
/// </summary>
/// <param name="hifile">格式</param>
/// <returns></returns>
public bool IsAllowedExtension(string hifile)
{
string[] strArray = new string[] { ".gif", ".jpg", ".JPG", ".GIF", ".jpeg", ".JPEG", ".bmp",".png", ".BMP", ".PNG"};
for (int i = 0; i < strArray.Length; i++)
{
if (hifile.Equals(strArray[i]))
{
return true;
}
}
return false;
}
实例图片


浙公网安备 33010602011771号