html:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImageTest.aspx.cs" Inherits="WebUI.ImageTest" %>
<!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>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>jquery实现按钮上传作者</title>
<style type="text/css">
#txtFileName { width: 300px;}
.btnsubmit{
border-bottom: #cc4f00 1px solid;
border-left: #ff9000 1px solid;
border-top: #ff9000 1px solid;
border-right: #cc4f00 1px solid;
text-align: center;
padding: 2px 10px;
line-height: 16px;
background: #e36b0f;
height: 24px;
color: #fff;
font-size: 12px;
cursor: pointer;
}
</style>
<script type="text/javascript" src="js/jquery-1.4.min.js"></script>
<script type="text/javascript" src="js/ajaxupload.js"></script>
<script type="text/javascript">
//支持图片格式png/jpg/gif/jpeg,文件大小不能超过500KB。
$(function () {
var button = $('#btnUp'), interval;
new AjaxUpload(button, {
//action:'upload-test.php',文件上传服务器端执行的地址
action: 'AjaxuploadHandler.ashx',
name: 'myfile',
onSubmit: function (file, ext) {
if (!(ext && /^(jpg|jpeg|JPG|JPEG|gif|GIF|png|PNG)$/.test(ext))) {
alert('图片格式不正确,请选择正确格式的文件!', '系统提示');
return false;
}
button.text('上传中');
this.disable();
interval = window.setInterval(function () {
var text = button.text();
if (text.length < 10) {
button.text(text + '|');
} else {
button.text('上传中');
}
}, 200);
},
onComplete: function (file, response) {
//file 本地文件名称,response 服务器端传回的信息
//button.text('上传图片(只允许上传JPG格式的图片,大小不得大于500K)' );
// button.text('重新上传' );
window.clearInterval(interval);
this.enable();
var k = response.replace("<PRE>", "").replace("</PRE>", "");
if (k == '-1') {
alert('您上传的文件太大啦!请不要超过500K');
}
else {
// alert( "服务器端传回的串:" +k);
// alert( "本地文件名称:" +file);
$("#txtFileName").val(k);
$("<img />").appendTo($('#imglist')).attr("src", k);
}
}
});
});
</script>
</head><body>上传图片:<input type="text" id="txtFileName" />
<div id="btnUp" style="width:300px;" class="btnsubmit" >浏览</div>
<div id="imglist"></div>
</body>
</html>
c# 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;using System.Text.RegularExpressions;
using System.IO;using System.Text;public class UploadHandler : IHttpHandler
{
private string _filedir = ""; //文件目录
/// <summary>
/// 处理上传文件(1:文件格式不正确、2:文件大小不正确、3:上传失败、文件名称:上传成功)
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
try
{
string result = "3";
// string fileType = context.Request.QueryString["fileType"]; //获取上传文件类型
string fileType = "img";
if (fileType == "file")
{
result = UploadFile(context); //文档上传
}
else if (fileType == "img")
{
result = UploadImg(context); //图片上传
}
context.Response.Write(result);
}
catch
{
context.Response.Write("3");//3文件上传失败
}
} #region 文档上传
/// <summary>
/// 文档上传
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private string UploadFile(HttpContext context)
{
int cout = context.Request.Files.Count;
if (cout > 0)
{
HttpPostedFile hpf = context.Request.Files[0];
if (hpf != null)
{
string fileExt = Path.GetExtension(hpf.FileName).ToLower();
//只能上传文件,过滤不可上传的文件类型
string fileFilt = ".rar|.zip|.pdf|.pdfx|.txt|.csv|.xls|.xlsx|.doc|.docx";
if (fileFilt.IndexOf(fileExt) <= -1)
{
return "1";
} //判断文件大小
int length = hpf.ContentLength;
if (length > 2097152)
{
return "2";
} Random rd = new Random();
DateTime nowTime = DateTime.Now;
string newFileName = nowTime.Year.ToString() + nowTime.Month.ToString() + nowTime.Day.ToString() + nowTime.Hour.ToString() + nowTime.Minute.ToString() + nowTime.Second.ToString() + rd.Next(1000, 1000000) + Path.GetExtension(hpf.FileName);
if (!Directory.Exists(_filedir))
{
Directory.CreateDirectory(_filedir);
}
string fileName = _filedir + newFileName;
hpf.SaveAs(fileName);
return newFileName;
} }
return "3";
}
#endregion /// <summary>
/// 图片上传
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private string UploadImg(HttpContext context)
{
string date = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); //_filedir = context.Server.MapPath(@"\\192.168.0.222\E:\upload");
_filedir = context.Server.MapPath(@"E:\upload");
int cout = context.Request.Files.Count;
if (cout > 0)
{
HttpPostedFile hpf = context.Request.Files[0];
if (hpf != null)
{
string fileExt = Path.GetExtension(hpf.FileName).ToLower();
//只能上传文件,过滤不可上传的文件类型
string fileFilt = ".gif|.jpg|.bmp|.jpeg|.png";
if (fileFilt.IndexOf(fileExt) <= -1)
{
return "1";
} //判断文件大小
int length = hpf.ContentLength;
if (length > 204800)
{
return "2";
} Random rd = new Random();
DateTime nowTime = DateTime.Now;
string newFileName = nowTime.Year.ToString() + nowTime.Month.ToString() + nowTime.Day.ToString() + nowTime.Hour.ToString() + nowTime.Minute.ToString() + nowTime.Second.ToString() + rd.Next(1000, 1000000) + Path.GetExtension(hpf.FileName);
if (!Directory.Exists(_filedir))
{
Directory.CreateDirectory(_filedir);
}
string fileName = _filedir + newFileName;
hpf.SaveAs(fileName);
return newFileName;
}
}
return "3";
}
#region IHttpHandler 成员 public bool IsReusable
{
get { throw new NotImplementedException(); }
} #endregion
}
没有任何反应。哪里错了。也不知道如何调试
<!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>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>jquery实现按钮上传作者</title>
<style type="text/css">
#txtFileName { width: 300px;}
.btnsubmit{
border-bottom: #cc4f00 1px solid;
border-left: #ff9000 1px solid;
border-top: #ff9000 1px solid;
border-right: #cc4f00 1px solid;
text-align: center;
padding: 2px 10px;
line-height: 16px;
background: #e36b0f;
height: 24px;
color: #fff;
font-size: 12px;
cursor: pointer;
}
</style>
<script type="text/javascript" src="js/jquery-1.4.min.js"></script>
<script type="text/javascript" src="js/ajaxupload.js"></script>
<script type="text/javascript">
//支持图片格式png/jpg/gif/jpeg,文件大小不能超过500KB。
$(function () {
var button = $('#btnUp'), interval;
new AjaxUpload(button, {
//action:'upload-test.php',文件上传服务器端执行的地址
action: 'AjaxuploadHandler.ashx',
name: 'myfile',
onSubmit: function (file, ext) {
if (!(ext && /^(jpg|jpeg|JPG|JPEG|gif|GIF|png|PNG)$/.test(ext))) {
alert('图片格式不正确,请选择正确格式的文件!', '系统提示');
return false;
}
button.text('上传中');
this.disable();
interval = window.setInterval(function () {
var text = button.text();
if (text.length < 10) {
button.text(text + '|');
} else {
button.text('上传中');
}
}, 200);
},
onComplete: function (file, response) {
//file 本地文件名称,response 服务器端传回的信息
//button.text('上传图片(只允许上传JPG格式的图片,大小不得大于500K)' );
// button.text('重新上传' );
window.clearInterval(interval);
this.enable();
var k = response.replace("<PRE>", "").replace("</PRE>", "");
if (k == '-1') {
alert('您上传的文件太大啦!请不要超过500K');
}
else {
// alert( "服务器端传回的串:" +k);
// alert( "本地文件名称:" +file);
$("#txtFileName").val(k);
$("<img />").appendTo($('#imglist')).attr("src", k);
}
}
});
});
</script>
</head><body>上传图片:<input type="text" id="txtFileName" />
<div id="btnUp" style="width:300px;" class="btnsubmit" >浏览</div>
<div id="imglist"></div>
</body>
</html>
c# 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;using System.Text.RegularExpressions;
using System.IO;using System.Text;public class UploadHandler : IHttpHandler
{
private string _filedir = ""; //文件目录
/// <summary>
/// 处理上传文件(1:文件格式不正确、2:文件大小不正确、3:上传失败、文件名称:上传成功)
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
try
{
string result = "3";
// string fileType = context.Request.QueryString["fileType"]; //获取上传文件类型
string fileType = "img";
if (fileType == "file")
{
result = UploadFile(context); //文档上传
}
else if (fileType == "img")
{
result = UploadImg(context); //图片上传
}
context.Response.Write(result);
}
catch
{
context.Response.Write("3");//3文件上传失败
}
} #region 文档上传
/// <summary>
/// 文档上传
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private string UploadFile(HttpContext context)
{
int cout = context.Request.Files.Count;
if (cout > 0)
{
HttpPostedFile hpf = context.Request.Files[0];
if (hpf != null)
{
string fileExt = Path.GetExtension(hpf.FileName).ToLower();
//只能上传文件,过滤不可上传的文件类型
string fileFilt = ".rar|.zip|.pdf|.pdfx|.txt|.csv|.xls|.xlsx|.doc|.docx";
if (fileFilt.IndexOf(fileExt) <= -1)
{
return "1";
} //判断文件大小
int length = hpf.ContentLength;
if (length > 2097152)
{
return "2";
} Random rd = new Random();
DateTime nowTime = DateTime.Now;
string newFileName = nowTime.Year.ToString() + nowTime.Month.ToString() + nowTime.Day.ToString() + nowTime.Hour.ToString() + nowTime.Minute.ToString() + nowTime.Second.ToString() + rd.Next(1000, 1000000) + Path.GetExtension(hpf.FileName);
if (!Directory.Exists(_filedir))
{
Directory.CreateDirectory(_filedir);
}
string fileName = _filedir + newFileName;
hpf.SaveAs(fileName);
return newFileName;
} }
return "3";
}
#endregion /// <summary>
/// 图片上传
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private string UploadImg(HttpContext context)
{
string date = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); //_filedir = context.Server.MapPath(@"\\192.168.0.222\E:\upload");
_filedir = context.Server.MapPath(@"E:\upload");
int cout = context.Request.Files.Count;
if (cout > 0)
{
HttpPostedFile hpf = context.Request.Files[0];
if (hpf != null)
{
string fileExt = Path.GetExtension(hpf.FileName).ToLower();
//只能上传文件,过滤不可上传的文件类型
string fileFilt = ".gif|.jpg|.bmp|.jpeg|.png";
if (fileFilt.IndexOf(fileExt) <= -1)
{
return "1";
} //判断文件大小
int length = hpf.ContentLength;
if (length > 204800)
{
return "2";
} Random rd = new Random();
DateTime nowTime = DateTime.Now;
string newFileName = nowTime.Year.ToString() + nowTime.Month.ToString() + nowTime.Day.ToString() + nowTime.Hour.ToString() + nowTime.Minute.ToString() + nowTime.Second.ToString() + rd.Next(1000, 1000000) + Path.GetExtension(hpf.FileName);
if (!Directory.Exists(_filedir))
{
Directory.CreateDirectory(_filedir);
}
string fileName = _filedir + newFileName;
hpf.SaveAs(fileName);
return newFileName;
}
}
return "3";
}
#region IHttpHandler 成员 public bool IsReusable
{
get { throw new NotImplementedException(); }
} #endregion
}
没有任何反应。哪里错了。也不知道如何调试
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货