標籤:style blog http io color os ar for sp
其實到現在我從剛畢業的雄心壯志,已經變為攀爬的小鳥了.雖然在沒有任務的情況下貪玩著,不過在接到任務的時,我還是抱著百分之百的心去完成.
在學校時,拖控制項是我最喜歡做的事,因為很簡單方便.從來沒有考略過效能之類的問題,絕對做出來我就很厲害了.但是現在到了公司完全是老鳥鄙視的對象,雖然公司沒有明確規定你不能拖控制項,但是可能是虛榮心吧,努力扔掉控制項,像js 進發. 我覺得我現在的狀態就是,出生的嬰兒,見一個學一個.
今天就總結一個我自己知道的圖片上傳.
js:
1 $(function () { 2 $(‘#hidIFrame‘).load(function () { 3 var retContent = $(‘#hidIFrame‘).get(0).contentWindow.document.body.innerText; 4 if (retContent == 0) { 5 alert(‘檔案不能上傳超過10M‘); 6 return false; 7 } 8 else { 9 10 $("#img").attr(‘src‘, ‘../img/‘ + retContent)11 }12 });13 $("#fil_img").change(function () {14 var img = $("#fil_img").val();15 if (img == "") {16 alert("請上傳圖片");17 return false;18 }19 else {20 if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(img)) {21 alert("圖片類型必須是.gif,jpeg,jpg,png中的一種")22 return false;23 }24 }25 $("#form1").submit();26 27 });28 });
頁面:
<form id="form1" method="post" action="../Handel/H_img.ashx" enctype="multipart/form-data" target="hidIFrame"> <div> <input type="file" id="fil_img" name="fil_img" /> </div> <div> <img id="img" src="" /> </div> </form> <iframe name="hidIFrame" id="hidIFrame" width="0" height="0" style="border: 0;"></iframe>
View Code
其實要做到在當頁面顯示最關鍵的就是iframe,在這過程中一定要在 form中 設定一個跟iframe 同名的target值.
在一般處理常式中存值:
private void addImg(HttpContext context) { string retVal = ""; HttpPostedFile file = context.Request.Files["fil_img"]; string _extensionName = ioFile.Path.GetExtension(file.FileName).TrimStart(‘.‘); //檔案不可以超過10M if (context.Request.Files["fil_img"].ContentLength > 1024 * 1204 * 10) { retVal = "0"; } else { //重新命名 retVal = Guid.NewGuid().ToString("N") + "_" + ioFile.Path.GetExtension(context.Request.Files["fil_img"].FileName); file.SaveAs(ioFile.Path.Combine(HttpContext.Current.Server.MapPath("/img/"), retVal)); } context.Response.Write(retVal); }View Code
圖片上傳_及時顯示