时间:2021-07-01 10:21:17 帮助过:3人阅读
<asp:Content ID="Content3" ContentPlaceHolderID="Head" runat="server">
<script src="<%=Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript"></script>
<script src="<%=Url.Content("~/Scripts/jquery.form.js") %>" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//上传图片
$("#btnUpload").click(function () {
if ($("#flUpload").val() == "") {
alert("请选择一个图片文件,再点击上传。");
return;
}
$('#UpLoadForm').ajaxSubmit({
success: function (html, status) {
var result = html.replace("<pre>", "");
result = result.replace("</pre>", "");
$("#image").attr('src', result);
alert(result);
}
});
});
});
ashx 如下:
代码如下:
namespace TestMvc.Utility
{
/// <summary>
/// Summary description for PicUploadHander
/// </summary>
public class PicUploadHander : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//验证上传的权限TODO
string _fileNamePath = "";
try
{
_fileNamePath = context.Request.Files[0].FileName;
//开始上传
string _savedFileResult = UpLoadImage(_fileNamePath, context);
context.Response.Write(_savedFileResult);
}
catch
{
context.Response.Write("上传提交出错");
}
}
注:整个上传使用ajax 异步数据,同时jquery回调出上传成功后图片在服务器上的相对路径。总的来说此方式相对传统的上传图片方式要强一些。
本例代码在此下载,FireFox 下测试通过。http://xiazai.jb51.net/201011/yuanma/AjaxUploadPic.rar