ajax与一般处理程序上传图片
前台html代码及juqery代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ajax上传文件.WebForm1" %>
一般处理程序代码: using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.Script.Serialization; namespace ajax上传文件 { /// <summary> /// Handler1 的摘要说明 /// </summary> public class Handler1 : IHttpHandler { public void ProcessRequest(HttpContext context) { HttpPostedFile _upfile = context.Request.Files["File1"]; if (_upfile == null) { ResponseWriteEnd(context,"4");//请选择要上传的文件 } else { string fileName = _upfile.FileName;/*获取文件名: C:Documents and SettingsAdministrator桌面123.jpg*/ string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();/*获取后缀名并转为小写: jpg*/ int bytes = _upfile.ContentLength;//获取文件的字节大小 if (suffix != "jpg") ResponseWriteEnd(context,"2"); //只能上传JPG格式图片 if (bytes > 1024 * 1024) ResponseWriteEnd(context,"3"); //图片不能大于1M _upfile.SaveAs(HttpContext.Current.Server.MapPath("")+"images" + fileName);//保存图片 ResponseWriteEnd(context,"1"); //上传成功 } } private void ResponseWriteEnd(HttpContext context,string msg) { context.Response.Write(msg); context.Response.End(); } public bool IsReusable { get { return false; } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |