87,910
社区成员
发帖
与我相关
我的任务
分享
<body>
<script type="text/javascript">
function check(f) {
alert("dd");
}
</script>
<iframe name="ajaxifr" style="display:none;"></iframe>
<form method="post" enctype="multipart/form-data" action="Handler/upload.ashx" target="ajaxifr" onsubmit="return check(this)">
<img src="" id="imgHead" />
文件描述:<input type="text" name="des" /><br/>
选择文件:<input type="file" name="upfile" /><br/>
<input type="submit" value="提交" />
</form>
<div id="ajaxMsg"></div>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HeadImg.Handler
{
/// <summary>
/// upload 的摘要说明
/// </summary>
public class upload : IHttpHandler
{
private string Js(string v) {//此函数进行js的转义替换的,防止字符串中输入了'后造成回调输出的js中字符串不闭合
if (v == null) return "";
return v.Replace("'", @"\'");
}
//下面就是一个简单的示例,保存上传的文件,如果要验证上传的后缀名,得自己写,还有写数据库什么的
public void ProcessRequest (HttpContext context) {
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
HttpServerUtility Server = context.Server;
//指定输出头和编码
Response.ContentType = "text/html";
Response.Charset = "utf-8";
HttpPostedFile f = Request.Files["upfile"];//获取上传的文件
string des = Request.Form["des"]//获取描述
,newFileName=Guid.NewGuid().ToString();//使用guid生成新文件名
if (f.FileName == "")//未上传文件
Response.Write(" ");//输出js,使用parent对象得到父页的引用
else { //保存文件
newFileName += System.IO.Path.GetExtension(f.FileName);//注意加上扩展名
try {
f.SaveAs(Server.MapPath("~/uploads/" + newFileName));//如果要保存到其他地方,注意修改这里
//调用父过程更新内容,注意要对des变量进行js转义替换,防止字符串不闭合提示错误
Response.Write(" ");
}
catch {
Response.Write(" ");//如果保存失败,输出js提示保存失败
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
<iframe name="ajaxifr" style="display:none;" onload="alert(this.contentWindow.document.body.innerHTML)"></iframe>