Uploadify使用随笔
最近项目使用了Uploadify一个上传插件,感觉的挺好用的。
上传控件:
引用JS
<script src='<% =ResolveUrl("~/JS/Uploadify/jquery.uploadify.js")%>' type="text/javascript"></script>
插件使用方法:
function UploadFY() {
var upload = {
'swf': '../JS/Uploadify/uploadify.swf',
'uploader': '../Ashx/FileOpHandler.ashx?Op=UploadFile&KGuid=' + Kguid + '&dType=SHPY_Files',
'buttonImg': '<% =ResolveUrl("~/JS/Uploadify/licon_001.gif")%>',
'cancelImg': '<% =ResolveUrl("~/JS/Uploadify/cancel.png")%>',
'queueID': Kguid,
'auto': true,
'multi': true,
'height': 22,
'width': 70,
'buttonText': '',
'fileTypeDesc': '系统支持文件', // The description for file types in the browse dialog
'fileTypeExts': '*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.rar;*.gd',
//需要重写的事件
'overrideEvents': ['onSelectError', 'onDialogClose'],
'multi': false, //是否允许多选
'auto': true, //是否允许自动上传
'method': 'GET',
'queueSizeLimit': 300, //同时上传数量
'uploadLimit': 10000, //一次浏览器课上成总数量
'fileSizeLimit': '10MB', //单个文件大小设置
'sizeLimit': 4000000,
'onQueueComplete': function (file) { //所有文件上传完成时触发此事件
},
'onSelect': function (file) {
//$('#fileupload').uploadifySettings('formData', { 'ASPSESSID': ASPSESSID, 'AUTHID': auth });
//alert(formDate);
fileName = file.name;
},
//返回一个错误,选择文件的时候触发
'onSelectError': function (file, errorCode, errorMsg) {
switch (errorCode) {
case -100:
alert("上传的文件数量已经超出系统限制的" + $('#fileupload').uploadify('settings', 'queueSizeLimit') + "个文件!");
return;
case -110:
alert("文件 [" + file.name + "] 大小超出系统限制的10MB大小!");
// alert("文件 [" + file.name + "] 大小超出系统限制的" + $('#fileupload').uploadify('settings', 'fileSizeLimit') + "大小!");
return;
case -120:
alert("文件 [" + file.name + "] 大小超出系统限制的10MB大小!");
return;
case -130:
alert("文件 [" + file.name + "] 类型不正确!");
return;
default:
alert("文件 [" + file.name + "] 文件检查发生错误!");
return;
}
},
//检测FLASH失败调用
'onFallback': function () {
alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");
},
'width': 34, //文件选择按钮大小
'height': 22,
'removeCompleted': true,
'removeTimeout': 0.1, //上传完成后自动消失时间/秒
'onQueueComplete': function (file) { //所有文件上传完成时触发此事件
if (fileName.length > 100) {
alert("文件名过长!");
return;
}
$("#hdAttachmentName").val(fileName);
$("#hdAttachmentPath").val('/uploads/SHPY_Files' + '/' + Kguid + '/' + fileName);
$("#<%=DivFileLink.ClientID %>").html('<a href="javascript:void(0)" onclick="Down()" >' + fileName + '</a> <img src="../JS/Uploadify/cancel.png" align="middle" onclick=DeleteFile() />');
}
}
return upload;
}
function LoadUpload() {
$("input[type='file']").each(function () {
$(this).uploadify((UploadFY()))
});
}
function Down() {
document.getElementById('btnDown').click();
}
function DeleteFile() {
$("#hdAttachmentName").val('');
$("#hdAttachmentPath").val('');
$("#<%=DivFileLink.ClientID %>").html('');
//此处可写触发删除文件
}
页面加载的时候调用:
LoadUpload();
FileOpHandler.ashx
上传代码:
private void UploadFile(HttpContext context)
{
try
{
HttpPostedFile file = context.Request.Files["Filedata"];
string KGuid = q(context, "KGuid");
string FileName = file.FileName;
int FileLength = file.ContentLength; ;
string FileType = file.FileName.Substring(file.FileName.LastIndexOf("."));
string CreateUserName = q(context, "CreateUserName");
string CreateUserID = q(context, "CreateUserID");
DateTime CreateDateTime = DateTime.Now;
string uploadPath = context.Server.MapPath("..\\uploads\\" + q(context, "dType") + "\\" + KGuid + "\\");
string FilePath = "..\\uploads\\" + q(context, "dType") + "\\" + KGuid + "\\" + FileName;
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);
//生成缩略图
}
context.Response.Write("1");
}
catch
{
context.Response.Write("0");
}
}
private string q(HttpContext context, string str)
{
if (context.Request[str] != null)
return context.Request[str].ToString();
return "";
}
public bool success
{
get;
set;
}
/// <summary>
/// Gets or sets the MSG.
/// </summary>
/// <value>The MSG.</value>
public string msg
{
get;
set;
}
/// <summary>
/// Gets or sets the public key.
/// </summary>
/// <value>The public key.</value>
public object data
{
get;
set;
}
文件下载:
protected void btnDown_Click(object sender, EventArgs e)
{
string filePath = Server.MapPath("../" + hdAttachmentPath.Value);
try
{
FileInfo info = new FileInfo(filePath);
if (!info.Exists)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "$.messager.alert('消息提示', '文件不存在!', 'info');", true);
return;
}
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode( info.Name));
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "$.messager.alert('消息提示', " + ex.Message + ", 'info');", true);
}
}
Uploadify使用随笔的更多相关文章
- 基于MVC4+EasyUI的Web开发框架形成之旅--附件上传组件uploadify的使用
大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用>中可以看到,Asp.NET中 ...
- uploadify.js
基于uploadify.js实现多文件上传和上传进度条的显示 uploadify是JQuery的一个插件,主要实现文件的异步上传功能,可以自定义文件大小限制.文件类型.是否自动上传等属性,可以显示上传 ...
- 上传组件uploadify的使用
上传组件uploadify的使用 大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用& ...
- 基于MVC4+EasyUI的Web开发框架形成之旅(4)--附件上传组件uploadify的使用
大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用>中可以看到,Asp.NET中 ...
- asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
上文件传很常见,现在就文件上传利用HTML的File控件(uploadify)的,这里为大家介绍一下(uploadify)的一些使用方法.在目前Web开发中用的比较多的,可能uploadify(参考h ...
- (转)基于MVC4+EasyUI的Web开发框架形成之旅--附件上传组件uploadify的使用
http://www.cnblogs.com/wuhuacong/p/3343967.html 大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随 ...
- ASP.NET Uploadify 上传文件过大报错
Uploadify上传文件原来很早之前用过,没发现什么问题.今天再使用过程中,当文件大于30M的时候就会报错404.查看错误消息提示配置最大上传太小了.需要修改. 记得原来配置上传文件大小在这里:&l ...
- ASP.NET Uploadify 上传文件过大 报错(http error)借鉴,以防忘记
Uploadify上传文件原来很早之前用过,没发现什么问题.今天再使用过程中,当文件大于30M的时候就会报错404.查看错误消息提示配置最大上传太小了.需要修改. 记得原来配置上传文件大小在这里:&l ...
- jquery.uploadify文件上传组件
1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...
随机推荐
- jupyter nb + scite 混合搭建成我的最爱IDE
jupyter nb + scite 混合搭建成我的最爱IDE 自从体验过jupyter notebook之后, 就深深地爱上了你, jupyter. jupyter这个名字也很古怪的. 它应该是ju ...
- CSRF token 无法被验证. ----Yii连接数据库后数据库错误日志报错
CSRF token 无法被验证. 我使用的是mongodb+ yii1.1 What is CSRF, please see the details here. http://en.wikiped ...
- volatile关键字并不能作为线程计数器
在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言是支持多线程的,为了解决线程并发的问题,在语 ...
- JAVA GUI
JAVA GUI中的事件处理: 委托事件模型:事件源对象和监听器对象具有绑定关系 一个监听器可以绑定多个事件源 一个事件源也可以绑定多个监听器 监听器有各自监听的事件类型 设置容器的布局管 ...
- 商业信息管理系统 Bizagi 建模pattern
WCP 1- Sequence This pattern is used to model dependencies between tasks so that one task cannot sta ...
- MySQL数据库的备份与还原
http://www.cnblogs.com/lql123/p/6090681.html //安装WordPress 1.备份 密码为:AAAzzz//123 mysqldump -uroot ...
- HTTP 错误 500.21 - Internal Server Error
HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipe ...
- Linux虚拟机中 Node.js 开发环境搭建
Node.js 开发环境搭建: 1.下载CentOS镜像文件和VMWare虚拟机程序; 2.安装VMWare——>添加虚拟机——>选择CentOS镜像文件即可默认安装带有桌面的Linux虚 ...
- Volley的GET和POST方法
首先记得加上权限 <uses-permission android:name="android.permission.INTERNET"/> XML代码 <?xm ...
- Linq to entities 学习笔记
Linq to entities ---提供语言集成查询支持用于在概念模型中定义的实体类型. 首先可以根据http://msdn.microsoft.com/en-us/data/jj206878该 ...