C#.net中jquery实现文件上传
2013-05-19 12:38
302 查看
页面代码:
NOTE:需要jquery插件(Uploadify)下载地址:uploadify下载
<%@ Page Language="C#" CodeBehind="AsynchronousUpload.aspx.cs"
Inherits="Example.AsynchronousUpload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/uploadify-v2.1.4/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="Scripts/uploadify-v2.1.4/jquery.uploadify.v2.1.4.js" type="text/javascript"></script>
<script src="Scripts/uploadify-v2.1.4/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>
<script src="Scripts/uploadify-v2.1.4/swfobject.js" type="text/javascript"></script>
<link href="Scripts/uploadify-v2.1.4/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function () {
$("#uploadify").uploadify({
'uploader': 'Scripts/uploadify-v2.1.4/uploadify.allglyphs.swf',
'script': 'handler/upHandle.ashx',
'cancelImg': 'Scripts/uploadify-v2.1.4/cancel.png',
'folder': 'UpFile', //上传文件路径
'queueID': 'fileQueue',
'auto': false, //是否需要点击上传才上传文件
'multi': false, //是否允许上传多个文件
'fileDesc': '请选择图片类型',
'fileExt': '*.jpg',
'sizeLimit': '1048576', //限制大小1M
onComplete: function (evt, queueID, fileObj, response, data) {
alert(response);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="fileQueue">
</div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<a href="javascript:$('#uploadify').uploadifyUpload()">上传</a>| <a href="javascript:$('#uploadify').uploadifyClearQueue()">
取消上传</a>
</p>
</form>
</body>
</html>
一般处理程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace Example.handler
{
/// <summary>
/// upHandle 的摘要说明
/// </summary>
public class upHandle : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath =HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);
//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
context.Response.Write("上传成功");
}
else
{
context.Response.Write("0");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
NOTE:需要jquery插件(Uploadify)下载地址:uploadify下载
<%@ Page Language="C#" CodeBehind="AsynchronousUpload.aspx.cs"
Inherits="Example.AsynchronousUpload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/uploadify-v2.1.4/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="Scripts/uploadify-v2.1.4/jquery.uploadify.v2.1.4.js" type="text/javascript"></script>
<script src="Scripts/uploadify-v2.1.4/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>
<script src="Scripts/uploadify-v2.1.4/swfobject.js" type="text/javascript"></script>
<link href="Scripts/uploadify-v2.1.4/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function () {
$("#uploadify").uploadify({
'uploader': 'Scripts/uploadify-v2.1.4/uploadify.allglyphs.swf',
'script': 'handler/upHandle.ashx',
'cancelImg': 'Scripts/uploadify-v2.1.4/cancel.png',
'folder': 'UpFile', //上传文件路径
'queueID': 'fileQueue',
'auto': false, //是否需要点击上传才上传文件
'multi': false, //是否允许上传多个文件
'fileDesc': '请选择图片类型',
'fileExt': '*.jpg',
'sizeLimit': '1048576', //限制大小1M
onComplete: function (evt, queueID, fileObj, response, data) {
alert(response);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="fileQueue">
</div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<a href="javascript:$('#uploadify').uploadifyUpload()">上传</a>| <a href="javascript:$('#uploadify').uploadifyClearQueue()">
取消上传</a>
</p>
</form>
</body>
</html>
一般处理程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace Example.handler
{
/// <summary>
/// upHandle 的摘要说明
/// </summary>
public class upHandle : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath =HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);
//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
context.Response.Write("上传成功");
}
else
{
context.Response.Write("0");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
相关文章推荐
- 一个简单的QQ隐藏图生成算法 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传
- 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传
- ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器<from Copying>
- C#/.NET RestSharp网络组件实现上传文件到远程服务器
- jQuery结合C#实现上传文件的方法
- asp.net(c#)实现文件上传
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- (转)ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
- Atitit 文件上传 架构设计 实现机制 解决方案 实践java php c#.net js javascript c++ python
- (原)ASP.NET 2.0(C#)实现多文件上传
- 在asp.net利用jquery.MultiFile实现多文件上传(转载)
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- 在asp.net利用jquery实现多文件上传
- asp.net,C#,html控件的File控件实现多文件上传简单实例,vs2010
- Asp.Net 用Jquery和一般处理程序实现无刷新上传大文件
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- JQuery多文件上传插件Uploadify在ASP.NET中的实现
- ASP.NET(C#)实现一次性上传多张图片(多个文件)