生活随笔
收集整理的這篇文章主要介紹了
ajax图片上传(asp.net +jquery+ashx)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、建立Default.aspx頁面
[csharp]?view plaincopy
<%@?Page?Language="C#"?AutoEventWireup="true"??CodeFile="Default.aspx.cs"?Inherits="_Default"?%>????<!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?runat="server">??????<title>ajax圖片上傳</title>??????<script?src="js/jquery-1.3.2.min.js"?type="text/javascript"></script>??????<script?src="js/jquery.form.js"?type="text/javascript"></script>????????????<script?type="text/javascript">??????????????function?upload(){??????????var?path?=?document.getElementById("File1").value;??????????var?img?=?document.getElementById("img1");??????????if($.trim(path)==""){??????????????alert("請選擇要上傳的文件");??????????????return;??????????????}????????????????????????$("#form1").ajaxSubmit({??????????????success:?function?(str)?{???????????????????if(str!=null?&&?str!="undefined"){??????????????????????if?(str?==?"1")?{alert("上傳成功");document.getElementById("img1").src="images/logo.jpg?"+new?Date();????????????????????else?if(str=="2"){alert("只能上傳jpg格式的圖片");}??????????????????????else?if(str=="3"){alert("圖片不能大于1M");}??????????????????????else?if(str=="4"){alert("請選擇要上傳的文件");}??????????????????????else?{alert('操作失敗!');}??????????????????}??????????????????else?alert('操作失敗!');??????????????},??????????????error:?function?(error)?{alert(error);},??????????????url:'Handler.ashx',?????????????type:?"post",?????????????dataType:?"text"?????????});??????}????????????</script>??</head>??<body>??????<form?id="form1"?runat="server">??????????<input?id="File1"?name="File1"?type="file"?/>??????????<input?id="iptUp"?type="button"?value="上傳Logo"??οnclick="upload()"/>??????????<img?id="img1"?alt="網站Logo"?src="images/weblogo.jpg"?/>??????</form>??</body>??</html>??
二、新建一個一般處理文件Handler.ashx
[csharp]?view plaincopy
<%@?WebHandler?Language="C#"?Class="Handler"?%>????using?System;??using?System.Web;????public?class?Handler?:?IHttpHandler?{????????????public?void?ProcessRequest?(HttpContext?context)?{??????????HttpPostedFile?_upfile?=?context.Request.Files["File1"];??????????if?(_upfile?==?null)??????????{??????????????ResponseWriteEnd(context,?"4");????????}??????????else??????????{??????????????string?fileName?=?_upfile.FileName;????????????string?suffix?=?fileName.Substring(fileName.LastIndexOf(".")?+?1).ToLower();????????????int?bytes?=?_upfile.ContentLength;??????????????if?(suffix?!=?"jpg")??????????????????ResponseWriteEnd(context,?"2");?????????????if?(bytes?>?1024?*?1024)??????????????????ResponseWriteEnd(context,?"3");???????????????_upfile.SaveAs(HttpContext.Current.Server.MapPath("~/images/logo.jpg"));????????????ResponseWriteEnd(context,?"1");?????????}??????}????????private?void?ResponseWriteEnd(HttpContext?context,?string?msg)??????{??????????context.Response.Write(msg);??????????context.Response.End();??????}????????????public?bool?IsReusable?{??????????get?{??????????????return?false;??????????}??????}??} ? 項目結構圖
?
轉載于:https://www.cnblogs.com/Jeremy2001/p/7226534.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的ajax图片上传(asp.net +jquery+ashx)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。