c#文件上传 如何调试AjaxuploadHandler.ashx

wangqiao208 2013-02-04 10:01:23
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
}




...全文
347 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
在相应的地方设置断点就可以了,调试和常规的调试没有区别的。
wangqiao208 2013-02-04
  • 打赏
  • 举报
回复
怎么没有人回复啊
内容概要:本文介绍了一种基于多目标粒子群算法(MOPSO)的微电网优化调度模型,综合考虑风能、光伏、储能系统、柴油发电机、燃气轮机以及与主电网之间的能量交互等多种分布式能源的协同运行。通过构建以运行成本最小化、碳排放最低化和系统可靠性最优化为目标的多目标优化模型,利用Matlab平台实现MOPSO算法求解,完成对微电网在不同运行场景下的能量管理与调度方案优化。该模型能够有效平衡经济性与环保性之间的关系,适用于含多类型分布式电源的复杂微电网系统,具有较强的工程应用价值和科研参考意义; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事微电网、智能电网、综合能源系统、可再生能源集成与优化调度等领域研究的专业人士; 使用场景及目标:①用于多能源耦合微电网系统的协同优化调度研究;②支持多目标智能优化算法在能源系统中的建模与求解实践,帮助用户掌握MOPSO在实际工程问题中的应用方法;③为学术论文复现、毕业设计、科研项目开发提供完整的代码实例与技术支撑; 阅读建议:建议读者结合Matlab代码与理论文档,深入理解目标函数构建、约束条件处理及Pareto最优解集生成机制,重点关注算法参数设置、多目标权衡分析与结果可视化,并可通过调整能源配置或引入新约束进行二次开发与创新研究。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧