(1)upload3.aspx-->引入两个十分重要的JS文件
<!--上传文件要用到的两个插件-->
<script type="text/javascript" src="/swfupload/swfupload.js"></script>
<script type="text/javascript" src="/swfupload/handlers.js"></script>
<script type="text/javascript" src="/js/jquery-1.9.1.js"></script>
<script type="text/javascript">
var swfu;
window.onload = function () {
//(2)在页面加载完成之后new了一个flash组件的对象
swfu = new SWFUpload({
//Backend Settings
upload_url: "upload3.ashx", //处理接收文件的一般处理程序
post_params: {
"ASPSESSID": "<%=Session.SessionID %>"
},
//文件上传的一些相关设置
file_size_limit: "2 MB", //DotNet客户默认只能向服务器端上传最大为4M的文件
file_types: "*.jpg", //上传文件的类型,多个可以用分号进行间格,例如: *.jpg;*.png
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: showData, //上传成功后执行的一个事件,默认是以上handler.js中的一个方法uploadSuccess,这里可以自定义
upload_complete_handler: uploadComplete,
//关于按钮的一些设置信息--------一个flash的上传按钮
button_image_url: "/swfupload/images/XPButtonNoText_160x22.png", //按钮用到的图片(路径不要写错了)
button_placeholder_id: "spanButtonPlaceholder", //给按钮占位的ID
button_width: 160, //按钮的宽与高
button_height: 22,
button_text: '<span class="button">Select Images <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设置
flash_url: "/swfupload/swfupload.swf", // Relative to this file
flash9_url: "/swfupload/swfupload_FP9.swf", // Relative to this file
custom_settings: {
upload_target: "divFileProgressContainer" //显示提示信息的占有位符的ID
},
// Debug Settings
debug: false
});
}
//--------------------serverData为上传文件成功之后,服务器返回的数据
function showData(file,serverData){
string array=serverData.split(':');
if(array[0]=='ok'){
$('#img1').attr('src',array[1]);
}
}
</script>
<body>
<span id="spanButtonPlaceholder"></span> <!--给按钮占位用的-->
<div id="divFileProgressContainer"></div> <!--服务器返回的提示信息的容器-->
<img id="img1"/>
</body>
(2)upload3.ashx
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType="text/Plain";
//接收从SWFUpload传过来的数据
HttpPostedFile file=context.Request.Files["Filedata"];
//判断文件是否为空
if(file!=null)
{
//判断文件名扩展名,并判断文件类型是否正确
string fileName=Path.GetFileName(file.FileName);
string fileExt=Path.GetExtension(fileName);
if(fileExt.ToLower()==".jpg")
{
file.SaveAs(context.Request.MapPath("/uploadImage/"+fileName));
context.Response.Write("ok:/uploadImage/"+fileName);
}
else
{
context.Response.Write("上传文件的类型格式不正确..");
}
}
else
{
context.Response.Write("上传的文件不能为空...");
}
}