找到资料包
没有的话就先去我的资源空间去下载
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",51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> ??????????????? // 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;
??????????????? },51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> ??????????????? // 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,51); font-family:Arial; font-size:14px; line-height:26px; text-align:left"> ??????????????? // 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文件夹,再往下找)