asp.net 上传图片(包括预览图,js)
来源:互联网 发布:网络女主播洗澡的视频 编辑:程序博客网 时间:2024/05/23 01:11
// js实现预览功能
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) { $('#img_prev' ).attr('src', e.target.result).width(544).height(168); };
reader.readAsDataURL(input.files[0]);
} else {
//IE下,使用滤镜
var docObj = document.getElementById('inputFile'); docObj.select();
var imgSrc = document.selection.createRange().text;
var localImagId = document.getElementById("localImag");
$( '#localImag').width(544).height(168); //必须设置初始大小
//图片异常的捕捉,防止用户修改后缀来伪造图片
try {
localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)" ;
localImagId.filters.item( "DXImageTransform.Microsoft.AlphaImageLoader" ).src = imgSrc;
} catch (e) {
alert( "您上传的图片格式不正确,请重新选择 !"); return false;
}
$( '#img_prev').hide();
document.selection.empty();
}
}
</script>
<body>
<form id="form1" runat="server">
<div>< span><strong >请首先指定Banner图片 :</strong ></span>
<asp: DropDownList ID ="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack ="true">
<asp: ListItem Selected ="True" Value ="0">新闻一</ asp:ListItem >
<asp: ListItem Value ="1">新闻二</ asp:ListItem >
<asp: ListItem Value ="2">新闻三</ asp:ListItem >
<asp: ListItem Value ="3">新闻四</ asp:ListItem >
<asp: ListItem Value ="4">广告一</ asp:ListItem >
<asp: ListItem Value ="5">广告二</ asp:ListItem >
<asp: ListItem Value ="6">广告三</ asp:ListItem >
<asp: ListItem Value ="7">广告四</ asp:ListItem >
</asp: DropDownList><br /><br />
<table width="65%" border="0" id="tab" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce">
<tr>
<td width="20%" height="30" bgcolor="#e1e5ee" style="text-align :left">
<div align="center">
<span class="STYLE10">请选择图片: <br />(图片大小 680*210)</span >
</div>
</td>
<td width="80%" height="20" bgcolor="#e1e5ee" style="text-align :left">
<input id="inputFile" style="width : 584px" type="file" onchange="readURL(this);" runat ="server"/>
<% -- <input type='file' id="doc" onchange="readURL(this);" /> -- %>
</td>
</tr>
<tr>
<td width="20%" height="20" bgcolor="#e1e5ee" style="text-align :left">
<div align="center">
<span class="STYLE10">预览图: </span></ div>
</td>
<td width="80%" bgcolor="#e1e5ee" style="text-align :left" height="20px">
<div id="localImag"><img id="img_prev" name="" src="" alt="your image" Height="168px" Width="544px" /></div>
<div style="display :none"> <asp: TextBox ID ="imgText" Text ="" Visible ="true" runat="server"></asp: TextBox></div >
<%-- <asp:Image ID="Image1" runat="server" Height="168px" Width="544px" />--%>
</td>
</tr>
<script type="text/javascript">
$(document).ready( function() {
$( '#img_prev').attr("src" , $('#imgText').val());
$( "#DropDownList1" ).change(function() {
$( '#img_prev').attr("src" , $('#imgText').val());
});
if ($("#DropDownList1" ).val() > 3) {
$( "#introTr").css("display" , "");
}
else {
$( "#introTr").css("display" , "none");
}
});
</script>
<tr id="introTr" style="display :">
<td width="20%" height="20" bgcolor="#e1e5ee" style="text-align :left">
<div align="center">
<span class="STYLE10">请输入文字: </span></ div>
</td>
<td width="80%" height="20" bgcolor="#e1e5ee" style="text-align :left">
<asp: TextBox ID ="TextBox1" runat ="server" Height ="140px" TextMode="MultiLine"
Width="443px"></asp: TextBox>
</td>
</tr>
<tr >
<td height="50" bgcolor="#e1e5ee" style="text-align :center">
</td>
<td height="50" bgcolor="#e1e5ee" style="text-align :center">
<asp: Button ID ="btnSave" runat ="server" Text ="上传" onclick="btnSave_Click" />
<asp: Button ID ="btnDelete" runat ="server" Text ="删除" onclick="btnDelete_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BizComponents;
using Entity;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
namespace Admin.Banner
{
public partial class BannerSet : System.Web.UI.Page
{
public int total;
public int sentTotal;
Banner_Biz biz = new Banner_Biz();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Banner info = getInfo();
TextBox1.Text = info.Introduction;
imgText.Text = "/images/" + info.Picture;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
string imgName = inputFile.Value;
if (!string .IsNullOrEmpty(imgName) || !string.IsNullOrEmpty(imgText.Text.Replace("/images/", "")))
{
string name = imgName;// 获取文件名
string type = name.Substring(name.LastIndexOf(".") + 1); //获取文件类型
string fileName = DateTime .Now.ToString("yyyyMMddHHmmss");
string ipath = Server.MapPath("../../images" ) + "\\" + fileName + "." + type; // 获取文件路径
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png" || type == "jpeg" || type == "")
{
if (inputFile.PostedFile.ContentLength <= 1024000)
{
Banner info = new Banner();
info.Picture = fileName + "." + type;
info.Introduction = TextBox1.Text;
if (Convert .ToInt32(DropDownList1.SelectedValue) >= 4)
{
info.IsNews = false;
info.Sort = Convert.ToInt32(DropDownList1.SelectedValue) - 3;
}
else
{
info.IsNews = true;
info.Sort = Convert.ToInt32(DropDownList1.SelectedValue) + 1;
}
string transfer = "BannerSet.aspx?_" + DateTime.UtcNow.Ticks;
try
{
int result = 0;
Banner info1 = getInfo();
if (string .IsNullOrEmpty(imgName))
{
info.Picture = info1.Picture;
}
if (info1 != null && info1.ID != 0)
{
info.ID = info1.ID;
info.Status = 1;
info.AddTime = info1.AddTime;
result = biz.Banner_Update(info);
}
else
{
result = biz.Banner_ADD(info);
}
if (result > 0)
{
if (!string .IsNullOrEmpty(imgName))
{
inputFile.PostedFile.SaveAs(ipath);
}
Response.Write( "<script>alert(' 图片上传成功! ');location.href='" + transfer + "'</script>" );
}
else
{
Response.Write( "<script>alert(' 图片上传失败! ')'</script>");
}
}
catch (Exception ex)
{
Logger.Error(" 图片上传出错 ", ex);
Response.Write( "<script>alert('" + ex.Message + "')</script>");
}
}
else
{
ClientScript.RegisterStartupScript( this.GetType(), "" , "alert('图片不能超时 1M')", true );
}
}
else
{
ClientScript.RegisterStartupScript( this.GetType(), "" , "alert('图片格式不正确 ')", true );
}
}
else
{
ClientScript.RegisterStartupScript( this.GetType(), "" , "alert('请选择上传图片 ')", true );
}
}
catch (Exception ex)
{
Logger.Error(" 上传图片失败 ", ex);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string transfer = "BannerSet.aspx?_" + DateTime.UtcNow.Ticks;
Banner info = new Banner();
if (Convert .ToInt32(DropDownList1.SelectedValue) >= 4)
{
info.IsNews = false;
info.Sort = Convert.ToInt32(DropDownList1.SelectedValue) - 3;
}
else
{
info.IsNews = true;
info.Sort = Convert.ToInt32(DropDownList1.SelectedValue) + 1;
}
info = biz.Banner_GetAll(info.Sort, info.IsNews);
if (info != null && info.ID != 0)
{
try
{
int result = biz.Banner_DelByID(info.ID);//调用删除方法
if (result > 0)
{
Response.Write( "<script>alert(' 删除图片成功! ');location.href='" + transfer + "'</script>" );
}
else
{
Response.Write( "<script>alert(' 删除图片失败! ')'</script>");
}
}
catch (Exception ex)
{
Logger.Error(" 删除图片出错 ", ex);
Response.Write( "<script>alert('" + ex.Message + "')</script>");
}
}
}
protected void DropDownList1_SelectedIndexChanged( object sender, EventArgs e)
{
Banner info = getInfo();
TextBox1.Text = info.Introduction;
imgText.Text = "/images/" + info.Picture;
}
public Banner getInfo()
{
Banner info = new Banner();
if (Convert .ToInt32(DropDownList1.SelectedValue) >= 4)
{
info.IsNews = false;
info.Sort = Convert.ToInt32(DropDownList1.SelectedValue) - 3;
TextBox1.Visible = true;
}
else
{
info.IsNews = true;
info.Sort = Convert.ToInt32(DropDownList1.SelectedValue) + 1;
TextBox1.Visible = false;
}
info = biz.Banner_GetAll(info.Sort, info.IsNews);
return info;
}
}
}
- asp.net 上传图片(包括预览图,js)
- asp.net+js实现上传图片即时预览
- asp.net+js实现上传图片即时预览
- ASP.NET上传图片时,产生预览
- ASP.NET上传图片时,产生预览
- ASP.NET上传图片时,产生预览
- ASP.NET上传图片时,产生预览
- asp.net C#图片上传+预览
- Asp.net上传图片产生预览效果
- ASP.NET上传图片时,产生预览
- ASP.NET上传图片时,产生预览
- Asp.net上传图片产生预览效果
- ASP.NET MVC图片上传前预览
- ASP上传图片预览
- JS预览上传图片
- js图片上传预览
- js 图片上传预览
- js上传图片预览 .
- MyEclipse中jar包的源代码
- 黑帽SEO常用作弊手法分析
- zend framework 数据库操作(DB操作)总结(转载收藏)
- Log4j使用指南
- 进程的状态
- asp.net 上传图片(包括预览图,js)
- sql server 时间戳
- yii中find简单用法
- 排序算法
- Fuzhou OJ Problem 1894 志愿者选拔
- INSTALL_FAILED_UPDATE_INCOMPATIBLE
- 基于HEVC的码率控制的相关提案的文献综述
- double free *** set a breakpoint in malloc_error_break to debug的另类错误原因
- 虚拟机下linux上网设置收获——亲试可行