用SWFUpload上传图片小例子
2024-09-06 20:22:25
在开发项目中,经常会用到上传图片,接下来我就用一种简单的方式给大家分享一下使用SWFUpload的方式上传图片。
1、在网站根目录下新建一个SWFUpload文件夹,把下载的组建放在SWFUpload文件夹中。SWFUpload包含的文件夹里会包含Images文件夹、handlers.js、swfupload.js、swfupload.swf、swfupload_fp9.swf。
2、把SWFUpload中的js引过来。
<script src="../js/jquery-1.7.1.js"></script>
<script src="../SWFUpload/swfupload.js"></script>
<script src="../SWFUpload/handlers.js"></script>
3、在JavaScript中为SWFUpload设置一些基本属性。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>上传文件</title>
<script src="../js/jquery-1.7.1.js"></script>
<script src="../SwfUpload/handlers.js"></script>
<script src="../SwfUpload/swfupload.js"></script>
<script type="text/javascript">
var swfu;
window.onload = function () {
swfu = new SWFUpload({
//后台设置。
//upload_url:处理上传文件的服务器端页面的url地址。
upload_url: "/ashx/Upload.ashx",
post_params: {
"ASPSESSID": "<%=Session.SessionID %>" //这里用到了一个SessionID,默认即可。
},
//上传文件大小。
file_size_limit: "2 MB",
//上传文件类型,这里可以上传的是jpg、gif、png、可以自己手动加,中间用分号隔开。
file_types: "*.jpg;*.gif;*.png",
//指定在文件选取窗口中显示的文件类型描述,起一个提示和说明的作用
file_types_description: "JPG Images",
//指定要上传的文件的最大体积,该属性为0时,表示不限制文件的大小
file_upload_limit: , //设置一些常用的执行函数。
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: uploadSuccess,
upload_success_handler: showImage,
//加载完成执行的函数。
upload_complete_handler: uploadComplete, //Button的一些常用设置。
//图片的路径。
button_image_url: "/SWFUpload/images/XPButtonNoText_160x22.png",
//图片的id。
button_placeholder_id: "spanButtonPlaceholder",
//图片的宽。
button_width: ,
//图片的高。
button_height: ,
button_text: '<span class="button">请选择上传图片<span class="buttonSmall">(最大为2MB)</span></span>',
button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
button_text_top_padding: ,
button_text_left_padding: , //Flash设置。在这里路径千万别错了。
flash_url: "/SWFUpload/swfupload.swf",
flash9_url: "/SWFUpload/swfupload_FP9.swf", custom_settings: {
upload_target: "divFileProgressContainer"
},
// Debug Settings
debug: false
});
} //上传成功以后执行的方法,此方法用于将上传的图片显示出来。
//serverData:服务端返回过来的数据。
function showImage(file, serverData) {
//为img的src属性赋值。
$("#showPhoto").attr("src", serverData);
} </script>
</head> <body>
<form id="form1" runat="server">
<div id="content">
<div id="swfu_container" style="margin: 0px 10px;">
<div> <span id="spanButtonPlaceholder"></span></div>
<div id="divFileProgressContainer" style="height: 75px;"></div>
<div id="thumbnails"></div>
<img id="showPhoto" style="width:300px;height:300px;"/>
</div>
</div>
</form>
</body>
</html>
4、接着写C#代码。
public class Upload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//获取文件。
HttpPostedFile file = context.Request.Files["Filedata"];
SaveImage(context,file);
} private void SaveImage(HttpContext context, HttpPostedFile _file)
{
if (_file != null)
{
//得到文件名。
string fileName = Path.GetFileName(_file.FileName);
//得到文件扩展名。
string fileExtension = Path.GetExtension(_file.FileName);
if (fileExtension.ToLower() == ".jpg")
{
//文件夹的格式为:UploadImage/年/月/日
string dir = "/UploadImage/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
//如果文件夹不存在,就创建。这个地方不要忘了写context.Request.MapPath
if (!Directory.Exists(context.Request.MapPath(dir)))
{
Directory.CreateDirectory(context.Request.MapPath(dir));
}
//创建一个Guid,目的是为了为上传上来的图片命名。
string newFileName = Guid.NewGuid().ToString();
//为图片制定完整路径。UploadImage/2017/8/21/guid.jpg。
string fullPath = dir + newFileName + fileExtension;
//保存。
file.SaveAs(context.Request.MapPath(fullPath));
//把物理路径返回回去。
context.Response.Write(fullPath);
}
}
} public bool IsReusable
{
get{return false;}
}
}
5、效果图。
5.1、运行成功以后,通过点击图片可以进行图片选择
结束。
最新文章
- Java导入的项目乱码怎么解决?(Ⅰ)
- RedHat/Centos修改root密码
- 用hexo书写github.io博客 学习心得 教程
- HTML5新特性之跨文档消息传输
- [原创]java WEB学习笔记54:Struts2学习之路--- 编写Struts2 的第一个程序,HelloWord,简述 package ,action,result
- oracle中的函数
- Jordan Lecture Note-4: Linear &; Ridge Regression
- Oracle procedure存储过程/function函数
- Razor视图引擎基础语法
- Log4Net Config Appender
- Exchange Server 2007的即将生命周期,您的计划是?
- 富文本编辑器UEditor自定义工具栏(二、插入图片、音频、视频个性化功能按钮和弹层及自定义分页符)
- ul 和 ol 标签的相关设置
- java————面向对象概念
- java创建线程的几种方式,了解一下
- Linux库多重依赖
- python中深拷贝和浅拷贝
- Lintcode481-Binary Tree Leaf Sum-Easy
- Record for Individual Project ( Word frequency program )
- 学习Spring Boot:(二十三)Spring Boot 中使用 Docker
热门文章
- 过滤身份证号和grep复习
- sed与grep练习题
- day9_7.9 函数的定义
- djang项目中的疑问及解决办法(ValueError: Invalid model reference &#39;apps.user.User&#39;. String model references must be of the form &#39;app_label.ModelName&#39;.)
- 使用java2Word生成Word文档打不开报错 存在非法字符xml
- 使用lombok的利弊
- DP问题(3) : hdu 1080
- DingTalk钉钉消息推送(.net core 3 WebApi尝鲜记)
- Visual Studio 2019(VS2019)正式版注册码秘钥
- Jenkins打包编码GBK的不可映射字符