AjaxFileUpload 在C#中应用
一、前台页面
<!DOCTYPE html> <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.8.2.min.js" type="text/javascript"></script>
<script src="js/ajaxfileupload.js"></script>
<script type="text/javascript">
function CheckFile() {
$.ajaxFileUpload({
url: 'upload.ashx?time=' + new Date(),
secureuri: false,
fileElementId: 'fuFileLoad', //上传控件ID
dataType: 'json', //返回值类型 一般设置为json
success: function (data, status) {
var jsonData = $.parseJSON(data);
alert(jsonData.Eor);
if (jsonData.Eor == "succss") {
alert(jsonData.Msg);
} else {
alert(jsonData.Msg);
}
},
error: function (data, status, e) {
alert(e); //就是在这弹出“语法错误”
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="fuFileLoad" runat="server" onchange="CheckFile();" />
</form>
</body>
</html>
二、上传功能代码 upload.ashx
using System;
using System.IO;
using System.Web;
using System.Web.Script.Serialization; public class upload : IHttpHandler { public void ProcessRequest (HttpContext context) {
//文件大小限制10M
const int maxSize = 10485760;
//定义允许上传的文件扩展名
var ext = new[] { "rar", "zip", "gif", "jpg", "jpeg", "png", "bmp", "xls", "xlsx", "doc", "docx", "et", "wps" };
string strExt = "";
foreach (var a in ext)
{
strExt += a + "/";
}
strExt = strExt.TrimEnd('/');
string savePath = "/upLoads/"; //文件保存路径 var resp = new UploadResponse();
HttpFileCollection imgFile = context.Request.Files;
if (imgFile.Count > 0)
{
string fileExt = Path.GetExtension(imgFile[0].FileName); //获得扩展名
if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(ext, fileExt.Substring(1).ToLower()) < 0)
{
resp.Eor = "error";
resp.Msg = string.Format("扩展名为{0}的文件不允许上传!\n只允许上传{1}格式的文件。", fileExt, strExt);
}
else
{
if (imgFile[0].InputStream.Length > maxSize)
{
resp.Eor = "error";
resp.Msg = "上传文件大小超过限制!";
}
else
{
string fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileExt; //新的文件名
try
{
SaveFile(imgFile[0], context.Server.MapPath(savePath), fileNewName); //保存文件 resp.Eor = "succss";
resp.Msg = "上传成功! 文件大小为:" + imgFile[0].ContentLength;
resp.ImgUrl = savePath + fileNewName;
resp.FName = fileNewName;
resp.OName = imgFile[0].FileName;
}
catch (Exception ex)
{
resp.Eor = "error";
resp.Msg = ex.ToString();
}
}
}
}
else
{
resp.Eor = "error";
resp.Msg = "请选择文件!";
}
context.Response.ContentType = "text/html";
context.Response.Write(new JavaScriptSerializer().Serialize(resp));
context.Response.End();
} private void SaveFile(HttpPostedFile imgFile, string savePath, string fileName)
{
if (!Directory.Exists(savePath)) //判断文件存放路径是否存在
{
Directory.CreateDirectory(savePath);
} imgFile.SaveAs(Path.Combine(savePath, fileName));
} public class UploadResponse
{
public string Eor { get; set; }
public string Msg { get; set; }
public string ImgUrl { get; set; }
public string FName { get; set; }
public string OName { get; set; } } public bool IsReusable {
get {
return false;
}
} }
三、返回的json数据调整

四、源代码下载地址:http://pan.baidu.com/s/1jGo1wzW
热烈欢迎大家吐槽。
AjaxFileUpload 在C#中应用的更多相关文章
- JQuery文件上传插件ajaxFileUpload在Asp.net MVC中的使用
0 ajaxFileUpload简介 ajaxFileUpload插件是一个非常简单的基于Jquery的异步上传文件的插件,使用过程中发现很多与这个同名的,基于原始版本基础之上修改过的插件,文件版本比 ...
- ajaxFileUpload增加附加参数
直接说方法: $.ajaxFileUpload({ data:{"a":123,"b":456};//附加参数,json格式 }); 然后在ajaxFileUp ...
- ajaxFileUpload 报这错jQuery.handleError is not a function 博客分类: WEB前端jquery
ajaxfileuploadhandleError 今天刚打个一个技术群,里面有人问标题上的问题,嘿,我恰好遇过,现在大家至少也在用jquery1.9以上的版本,ajaxfileupload的版本早 ...
- ajaxFileUpload 报这错jQuery.handleError is not a function
今天刚打个一个技术群,里面有人问标题上的问题,嘿,我恰好遇过,现在大家至少也在用jquery1.9以上的版本,ajaxfileupload的版本早就不更新了,大家可以下载看:地址这里, 它例子里使用的 ...
- JS ajaxfileUpload 一次性上传多个input控件 上传多个文件
本方法适用于一次性上传多个input框输入的文件,如下图所示,任务是需要一次上传两个input框提供的两个文件. 具体方法: 1.修改ajax调用方法 如上图所示,只需要将ajaxFileUpload ...
- ajaxfileupload插件,C#返回Json数据报错
报错信息一:jQuery.handleError is not a function 上传图片的时候,通过F12,查看到这个错误. 解决方案: jquery版本问题,handlerError只在jqu ...
- 【转载】ajaxFileUpload 报这错jQuery.handleError is not a function
今天刚打个一个技术群,里面有人问标题上的问题,嘿,我恰好遇过,现在大家至少也在用jquery1.9以上的版本,ajaxfileupload的版本早就不更新了,大家可以下载看:地址这里,它例子里使用的J ...
- jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法(ajaxfileupload.js第四弹)
第四弹的诞生完全不在自己最初的计划之中,是有个网友看了先前关于<ajaxfileupload.js系列>的文章后提出的问题,由于自己一直是用chrome浏览器去测试demo,完全忽略IE浏 ...
- input file 图片上传
使用第三方:jquery.ajaxfileupload.jsinput中的name根据后端来定 <form method="post" enctype="multi ...
随机推荐
- iOS GCD 与 NSOperationQueue
NSOperationQueue ios NSOperation vs. GCD StackOverflow: NSOperation vs. Grand Central Dispatch Blog: ...
- stm32的DFU使用方法
stm32的dfu看上去是个很高级的东西,似乎可以通过USB给内部flash.外部spi flash.外部nor等东西刷写数据.把数据读出来,但是用了一下感觉确实有点麻烦. 先不管原理是怎样的,使用方 ...
- Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改
A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...
- 从零开始学习Hadoop--第2章 第一个MapReduce程序
1.Hadoop从头说 1.1 Google是一家做搜索的公司 做搜索是技术难度很高的活.首先要存储很多的数据,要把全球的大部分网页都抓下来,可想而知存储量有多大.然后,要能快速检索网页,用户输入几个 ...
- 查看修改swap空间大小
1 查看swap 空间大小(总计): # free -m 默认单位为k, -m 单位为M total used fr ...
- java_SSH整合1
Domain: public class Department { private Long id; private Set<User> users = new HashSet<Us ...
- c#_表单处理方式
阅读目录 开始 简单的表单,简单的处理方式 表单提交,成功控件 多提交按钮的表单 上传文件的表单 MVC Controller中多个自定义类型的传入参数 F5刷新问题并不是WebForms的错 以Aj ...
- linux 安装xamp
前一久用上了ubuntu,想折腾下小窝,懒得自己去装Php啊,apache 之类的东西,刚才用上xampp,直接点,等以后要涉及深再弄,暂时先用着xampp.还不错,很好用,这里简单说下安装,(我是新 ...
- Helpers\Sessions
Helpers\Sessions The session is a static class, this means it can be used in any controller without ...
- Debian下的PPPOE服务器配置
参考: http://blog.csdn.net/zhangwenjianqin/article/details/7655375 http://blog.sina.com.cn/s/blog_8043 ...