C#實現(xiàn)圖片上傳與瀏覽切換的方法
更新時間:2014年08月25日 11:09:09 投稿:shichen2014
這篇文章主要介紹了C#實現(xiàn)圖片上傳與瀏覽切換的方法,是很有實用價值的一個應(yīng)用技巧,需要的朋友可以參考下
本文以一個完整實例講述了C#實現(xiàn)圖片上傳與瀏覽切換的方法,對于進行C#程序設(shè)計來說具有一定的借鑒價值。分享給大家供大家參考。
具體實現(xiàn)代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!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>無標題頁</title> <script src="js/jquery-1.7.2.js" type="text/javascript"></script> <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $('.upic').change(function() { var filename = $(this).val().replace(/.*(\/|\\)/, ""); //文件名 var fileExt = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : ''; //后綴名 if (fileExt != "jpeg" && fileExt != "jpg" && fileExt != "gif" && fileExt != "png") { alert('請選擇一個jpg或gif的圖片文件'); return; }; $(this).parent().parent().parent().addClass('support'); if ($.browser.msie) { if ($.browser.version == "6.0") { $(this).parent().parent().siblings(".preview").html('<img style="height:auto;width:88px;" src="' + $(this).val() + '"/>'); } else { var previewDiv = $(this).parent().parent().siblings(".preview"); previewDiv.css({ filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)" }); previewDiv[0].filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "scale"; try { previewDiv[0].filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = getPath($(this)[0]); } catch (e) { alert(e.name + ": " + e.message); alert("無效的圖片文件!"); return; } } } else { //var data=$(this)[0].files.item(0).getAsDataURL(); var data = window.URL.createObjectURL($(this)[0].files[0]); $(this).parent().parent().siblings(".preview").html('<img style="height:auto;width:88px;" src="' + data + '"/>'); return; } $(this).parent().parent().parent().mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); }); }); $('.del').click(function() { $(this).parent().siblings("[name='isDel']").val("1"); var file = $(this).parent().siblings(".upload").children().children(".upic"); file.after(file.clone(true).val("")).remove(); $(this).parent().siblings(".preview").after('<div class="preview"></div>').remove(); //$(this).parent().siblings(".preview").empty(); $(this).parent().parent().removeClass('support').mouseout().unbind('mouseover'); }); if ($.browser.msie) { $('#memberPhoto').filter('.support').mouseover(function() { $(this).addClass('hover'); }).mouseout(function() { $(this).removeClass('hover'); }); }; //獲得上傳控件的值,obj為上傳控件對象 function getPath(obj) { if (obj) { if (window.navigator.userAgent.indexOf("MSIE") >= 1) { obj.select(); document.getElementById("btnSave").focus(); //hack for IE9下,如果file控件獲得焦點,則document.selection.createRange()拒絕訪問 return document.selection.createRange().text; } else if (window.navigator.userAgent.indexOf("Firefox") >= 1) { if (obj.files) { return obj.files.item(0).getAsDataURL(); } return obj.value; } return obj.value; } } }); </script> </head> <body> <form id="form1" runat="server"> <div id="pic"> <div id="memberPhoto" style="position: relative"> <%if (!string.IsNullOrEmpty(imagePath)) {%> <div class="preview"> <img src="../<%=imagePath%>" style="height: auto; width: 88px;" /></div> <%} else {%> <div class="preview"> </div> <%}%> <div class="upload"> <a class="a-sc" href="javascript:;"> <input id="imageUpload" class="upic" type="file" name="upic" runat="server" /> </a> </div> <div class="act"> <span class="del">刪除</span> </div> </div> </div> </form> </body> </html>
public partial class Default3 : System.Web.UI.Page { public string imagePath = ""; protected void Page_Load(object sender, EventArgs e) { } }
希望本文實例對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
關(guān)于finalize機制和引用、引用隊列的用法詳解
下面小編就為大家?guī)硪黄P(guān)于finalize機制和引用、引用隊列的用法詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09Windows下C#的GUI窗口程序中實現(xiàn)調(diào)用Google Map的實例
這篇文章主要介紹了Windows下C#的GUI窗口程序中實現(xiàn)調(diào)用Google Map的實例,如果只想調(diào)用瀏覽器打開網(wǎng)頁的話可以看文章最后的方法,需要的朋友可以參考下2016-04-04用?FieldMask?提高?C#?gRpc?的服務(wù)性能
這篇文章主要介紹了用?FieldMask?提高?C#?gRpc?的服務(wù)性能,FieldMask?是一個?protobuf?消息,包含一個名為?paths?的字段,用于指定用于指定讀取操作返回或更新操作修改的字,下文詳細內(nèi)容,需要的朋友可以參考一下2022-03-03