一、引入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. 关于<![CDATA[]]

    术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data). 在 XML 元素中,"<" 和 "&& ...

  2. iOS - KVC 键值编码

    1.KVC KVC 是 Key-Value Coding 的简写,是键值编码的意思,属于 runtime 方法.Key Value Coding 是 cocoa 的一个标准组成部分,是间接给对象属性设 ...

  3. 我眼中BA(业务需求分析师)的技能广度和深度

    BA,或者称业务分析师,是企业数字能力和业务能力之间的沟通桥梁.随着企业数字转型的进一步深化,相信对BA这样的技能需求会越来越多,只是未必都用“BA/业务分析师”这样的Title. ThoughtWo ...

  4. spring集成quartz scheduler

    创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean  Quartz Scheduler 配置作业(MethodInvokingJobD ...

  5. Java 多线程 (转)

    http://www.ibm.com/developerworks/cn/java/j-thread/index.html http://www.ibm.com/developerworks/cn/j ...

  6. mysql 占用的内存大小

    1.mysql执行查询计划,key_len表示索引使用的字节数,这个字节数和三个条件有关.mysql> create table t1(v1 char(10));Query OK, 0 rows ...

  7. js-------》(小效果)实现倒计时及时间对象

    js实现倒计时及时间对象 JS实现倒计时效果代码如下: 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset ...

  8. CodeForces 185A 快速幂

    一开始找矩阵快速幂的题来做时就看到了这题,题意就是让你求出如图所示的第n个三角形中指向向上的小三角形个数.从图中已经很容易看出递推关系了,我们以f[n]表示第n个大三角形中upward的小三角形个数, ...

  9. linux简单配置

    lsof -i lsof -i:211.判断apache查找httpd路径: ps aux | grep httpd 结果:  /usr/local/apache/bin/httpd /usr/loc ...

  10. idea+git

    http://www.cnblogs.com/java-maowei/p/5950930.html