您的位置:首页 > 编程语言 > ASP

swf上传控件在asp.net和MVC中的运用

2009-11-30 17:11 344 查看
找到资料包

没有的话就先去我的资源空间去下载

http://download.csdn.net/source/1860028

里面包含3种文件

(1)Ashx

(2)SWF

(3)swfuoload.js

(4)XPButtonNoText_160x22.png图片

把他复制到项目中,具体是哪,可以自己根据需求放

使用方法:

(1):引入swfupload.js文件

<script type="text/javascript" src="资料包/swfupload.js"></script>

(2):复制下面代码到项目中。

<script type="text/javascript">

var swfu;

window.onload = function() {

swfu = new SWFUpload({

// Backend Settings

upload_url: "资料包/Ashx/Upload.ashx",

post_params: {

"ASPSESSID": "<%=Session.SessionID %>"

},

// File Upload Settings

file_size_limit: "5 MB",

file_types: "*.jpg;*.png;*.gif;*.bmp;*.jpeg",

file_types_description: "JPG Images",

// Event Handler Settings

file_dialog_complete_handler: function(numFilesSelected, numFilesQueued) { if (numFilesQueued > 0) this.startUpload(); },

upload_success_handler: function(file, responseText) {

alert("图片上传成功!上传之前的名称为:" + file.name + "上传之后的文件名称为:" + responseText);

document.getElementById("path").value = file.name;

},

// Button settings

button_image_url: "~/资料包/XPButtonNoText_160x22.png",

button_placeholder_id: "spanButtonPlaceholder",

button_width: 160,

button_height: 22,

button_text: '点击浏览上传(<3M)',

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: "资料包/Swf/swfupload.swf", // Relative to this file

// Debug Settings

debug: false

});

}

</script>

其中:

upload_url:为ashx的路径

file_size_limit: 上传的最大限制

file_types:上传的类型

file_types_description:上传的文件类型说明

upload_success_handler: function(file, responseText) {}文件上传成功之后的事情。可以根据需求在里面写内容

其中参数 file.name 文件上传前的名称

responseText 为 文件上传之后的名称

button_placeholder_id: 控件的ID

flash_url 为swfupload.swf的路径

其他的都很简单,就不一一介绍了。

请根据自己的具体情况,修改上面的参数

(3)找到ashx文件夹中的Upload.ashx

找到这句代码upload.SaveAs(Path.Combine(context.Server.MapPath("upload"), name));

这里保存的绝对路径。context.Server.MapPath("upload")的意思是项目Upload.ashx当前的路径的upload的文件夹下,name为文件名

我们再跟目录下建一个文件夹upload用来存放上传的文件。

更改 upload.SaveAs(Path.Combine(context.Server.MapPath("../../upload"), name));

这样上传的文件就被保存在upload文件夹下了。

(4)设置配置文件 web.config

在</system.web>节点下面添加

<location path="资料包/Ashx/Upload.ashx">

<system.web>

<httpRuntime maxRequestLength="3100" executionTimeout="300"/>

</system.web>

</location>

其中maxRequestLength="3100" 为最大限制3M 超时为executionTimeout="300"

(5)使用的时候加上下面的代码:

<div style="float:left"><input type="text" id="Path" /></div>

<div style="float:left"><div id="spanButtonPlaceholder"></div></div>

到这里上传控件就已经完成了!点击运行,你学会了吗?

( MVC的和这个很类似,就不再做详细说明了。注意,在MVC中 <input type="text" id="path" />ID不能为path关键字

还有MVC的Content文件夹下的目录如果要用相对路径的话,最好是先返回到Content文件夹,再往下找)

另外注意:swf在ie9 和ie10非兼容性模式下不显示的bug解决方法如下:

找到swfuoload.js ,找到里面的方法:SWFUpload.prototype.getFlashHTML ,把方法替换为以下的代码:

// Private: getFlashHTML generates the object tag needed to embed the flash in to the document
SWFUpload.prototype.getFlashHTML = function () {
// Flash Satay object syntax: http://www.alistapart.com/articles/flashsatay var myclass = "";
if (navigator.userAgent.indexOf("MSIE") > 0) {
myclass = 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
}

return ['<object ' + myclass + '  id="', this.movieName, '" type="application/x-shockwave-flash" data="', this.settings.flash_url, '" width="', this.settings.button_width, '" height="', this.settings.button_height, '" class="swfupload">',
'<param name="wmode" value="', this.settings.button_window_mode, '" />',
'<param name="movie" value="', this.settings.flash_url, '" />',
'<param name="quality" value="high" />',
'<param name="menu" value="false" />',
'<param name="allowScriptAccess" value="always" />',
'<param name="flashvars" value="' + this.getFlashVars() + '" />',
'</object>'].join("");
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: