当前位置: 编程技术>.net/c#/asp.net
C#实现图片上传与浏览切换的方法
来源: 互联网 发布时间:2014-11-02
本文导语: 本文以一个完整实例讲述了C#实现图片上传与浏览切换的方法,对于进行C#程序设计来说具有一定的借鉴价值。分享给大家供大家参考。 具体实现代码如下: 无标题页 $(document).ready(function() { $('.upic')...
本文以一个完整实例讲述了C#实现图片上传与浏览切换的方法,对于进行C#程序设计来说具有一定的借鉴价值。分享给大家供大家参考。
具体实现代码如下:
无标题页
$(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('
');
} 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('
');
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('').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;
}
}
});
public partial class Default3 : System.Web.UI.Page
{
public string imagePath = "";
protected void Page_Load(object sender, EventArgs e)
{
}
}
希望本文实例对大家的C#程序设计有所帮助。