一、引入js文件
<script type="text/javascript" src="/Scripts/JQuery.min.js"></script>
<script src="/scripts/AjaxUpload.3.5.js" type="text/javascript"></script>

二、Upload.ashx代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.IO;
using System.Globalization;
using LitJson;
using System.Web.SessionState; namespace HuWaiTong.Ashx
{
/// <summary>
/// UploadFile 的摘要说明
/// </summary>
public class Upload : IHttpHandler, IRequiresSessionState
{
private HttpContext context;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//指定文件保存目录
string getPath = context.Request.QueryString["p"] ;
//文件保存目录路径
string savePath = "/upload/" + getPath + "/";
//文件保存目录URL
string saveUrl = "/upload/" + getPath + "/";
//定义允许上传的文件扩展名
Hashtable extTable = new Hashtable();
extTable.Add("image", "gif,jpg,jpeg,png,bmp");
extTable.Add("flash", "swf,flv");
extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,amr,rm,rmvb");
extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,css");
//最大文件大小
string dirName = context.Request.QueryString["dir"];
int maxSize = ; if (dirName == "media")
{
maxSize = ;
}
this.context = context; HttpPostedFile imgFile = context.Request.Files["imgFile"]; if (imgFile == null)
{
showError("请选择文件。");
} string dirPath = context.Server.MapPath(savePath); if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
} if (string.IsNullOrEmpty(dirName))
{
dirName = "image";
} if (!extTable.ContainsKey(dirName))
{
showError("目录名不正确。");
} string fileName = imgFile.FileName;
string fileExt = Path.GetExtension(fileName).ToLower(); if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
{
showError("上传文件大小超过限制。");
} if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(((string)extTable[dirName]).Split(','), fileExt.Substring().ToLower()) == -)
{
showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((string)extTable[dirName]) + "格式。");
} //创建文件夹
dirPath += dirName + "/";
saveUrl += dirName + "/"; if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
} string ymdStr = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
dirPath += ymdStr + "/";
saveUrl += ymdStr + "/"; if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
} string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
string filePath = dirPath + newFileName; imgFile.SaveAs(filePath); string fileUrl = saveUrl + newFileName; Hashtable hashMsg = new Hashtable();
hashMsg["error"] = ;
hashMsg["url"] = fileUrl; context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8"); //转相对路径
//2013-11-29 11:17:46 谢俊
if (!string.IsNullOrEmpty(context.Request["relative"]))
{
var relativeUrl = hashMsg["url"].ToString().Substring(, hashMsg["url"].ToString().IndexOf("upload"));
hashMsg["url"] = hashMsg["url"].ToString().Replace(relativeUrl, "../../../");
} context.Response.Write(JsonMapper.ToJson(hashMsg));
context.Response.End();
} private void showError(string messageStr)
{
Hashtable hashMsg = new Hashtable();
hashMsg["error"] = ;
hashMsg["message"] = messageStr;
context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
context.Response.Write(JsonMapper.ToJson(hashMsg));
context.Response.End();
} public bool IsReusable
{
get
{
return false;
}
}
}
}

三使用

脚本

<script>
new AjaxUpload($("#upload_id"), {
action: '/Ashx/Upload.ashx?p=page&dir=image',
name: 'imgFile',
onSubmit: function (file, ext) {
if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
$.messager.alert('错误提示', '只能上传 JPG, PNG 或 GIF 类型的文件!', 'info'); return false;
}
},
onComplete: function (file, response) {
response = eval("(" + response + ")");
if (response.error != "0") {
alert(response.message);
} else {
document.getElementById("#show_id").setAttribute("src", response.url);
document.getElementById("#value_id").setAttribute("value", response.url);
}
}
});
</script>

html代码

<img id="show_id" src="" >
<input id="value_id" value="" type="hidden">
<a id="upload_id" ">选择图片</a>

AjaxUpload.3.5.js之ASP.NET 文件上传的更多相关文章

  1. asp.net 文件上传示例整理

    ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录.  代码如下 复制代码 ...

  2. Selenium常用API用法示例集----下拉框、文本域及富文本框、弹窗、JS、frame、文件上传和下载

    元素识别方法.一组元素定位.鼠标操作.多窗口处理.下拉框.文本域及富文本框.弹窗.JS.frame.文件上传和下载 元素识别方法: driver.find_element_by_id() driver ...

  3. IIS7.5修改asp的文件上传限制方法

    第一.IIS7.5修改asp的文件上传限制方法 1.打开IIS 2.打开面板中的应用程序开发 asp 3.找到最后的限制属性 4.修改其中的最大请求实体主体限制的值:默认为200000字节,等于195 ...

  4. asp.net文件上传进度条研究

    文章:asp.net 文件上传进度条实现代码

  5. Cookie操作、ASP.Net文件上传HttpPostedFile

    概述 Cookie用来保存客户浏览器请求服务器页面的请求信息. 我们可以存放非敏感的用户信息,保存时间可以根据需要设置.如果没有设置Cookie失效日期,它的生命周期保存到关闭浏览器为止,Cookie ...

  6. js 实现 input file 文件上传

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  7. 前端使用 js 如何实现大文件上传

    前端使用 js 如何实现大文件上传 大文件上传 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  8. 你以为的ASP.NET文件上传大小限制是你以为的吗

    我们以为的文件大小限制 我们大家都知道ASP.NET为我们提供了文件上传服务器控件FileUpload,默认情况下可上传的最大文件为4M,如果要改变可上传文件大小限制,那么我们可以在web.confi ...

  9. ASP.NET文件上传大小的限制解决方案

    我们大家都知道ASP.NET为我们提供了文件上传服务器控件FileUpload,默认情况下可上传的最大文件为4M,如果要改变可上传文件大小限制,那么我们可以在web.config中的httpRunti ...

随机推荐

  1. redis系列

    1.redis windows下的环境搭建 2.redis初学系列

  2. Redis基础知识之————使用技巧(持续更新中.....)

    一.key 设计技巧 把表名转换为key前缀 如, tag: 第2段放置用于区分区key的字段--对应mysql中的主键的列名,如userid 第3段放置主键值,如2,3,4...., a , b , ...

  3. Redis实践操作之—— keyspace notification(键空间通知)

    一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何对过期Key进行一个回调处理? 如何使用 Redis 来实现定时任务? 二.序言: 本文所说的定时任务 ...

  4. SQL 调优专题总结

    oracle 的优化器: oracle 有两种优化器:基于规则的优化器(rbo/rule based optimizer)和基于代价的优化器(cbo/cost based optimizer). 有时 ...

  5. Java 7 命令/工具 jcmd 使用详细解释

    常见功能 列出 Java 进程 PID 以及 名称 列出进程的 thread dump 得到进程的 heap dump 得到进程的 JVM 参数 具体如下: 列出 Java 进程 PID 以及 名称 ...

  6. perl中->和=>作用

    -> 用法 -> 有两种用法,都和解引用有关. 第一种用法,就是解引用. 根据 -> 后面跟的符号的不同,解不同类型的引用, ->[] 表示解数组引用,->{} 表示解散 ...

  7. a、b交换与比较

    1.有两个变量a,b,不用if.?: .switch 或其他判断语句,找出两个数中 较大的: int max = ((a+b)+abs(a-b))/2 较小的: int min = ((a+b)-ab ...

  8. 例题:超市买东西的程序。输入商品信息,计算价格,价格满多少元打折。这道题用到结构体,集合,for循环,if else语句

    知识要点: 集合和数组的区别:数组是连续的,同一类型的一块区域,而集合可以是不连续的,多种数据类型的. 集合属性:.count 方法:.Add()  将对象添加到ArrayList中实际包含的元素数 ...

  9. ServletInputStream的重复读取(多次读取)(转)

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  10. 核心Javascript学习

    1. 引言: 1.1. 网页三要素: l HTML(内容) l CSS(外观) l Javascript(行为) 1.2.  OOP的相关概念 1). 对象,方法和属性 l 对象就是指"事物 ...