前台:
function checkType() {
if ($("#FUp_Picture").val() == "") {
return;
}
//得到上传文件的值
var fileName = document.getElementById("FUp_Picture").value;
//返回String对象中子字符串最后出现的位置.
var seat = fileName.lastIndexOf(".");
// alert("返回String对象中子字符串最后出现的位置" + seat);
//返回位于String对象中指定位置的子字符串并转换为小写.
var extension = fileName.substring(seat).toLowerCase();
// alert("返回位于String对象中指定位置的子字符串并转换为小写" + extension);
//判断允许上传的文件格式
var allowed = [".jpg", ".gif", ".png", ".bmp", ".jpeg"];
for (var i = 0; i < allowed.length; i++) {
if (!(allowed[i] != extension)) {
return true;
}
}
alert("不支持" + extension + "格式");
return false;
}
<div id="Pic" closable="false" style="overflow: auto; width: 475px; height: 280px;
margin-left: 10px;">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td colspan="3">
<asp:Image ID="IgMeet_Pic" runat="server" Width="465px" Height="370px" />
</td>
</tr>
<tr>
<td height="5px">
</td>
</tr>
<tr>
<td colspan="3">
<asp:FileUpload ID="FUp_Picture" runat="server" BackColor="White" />
</td>
</tr>
</table>
</div>
<asp:Button ID="btn_update" runat="server" Text="确认" Height="20px" Width="88px" OnClick="btn_update_Click"
OnClientClick="checkType();" />
后台:
protected void btn_update_Click(object sender, EventArgs e)
{
///链接 数据库 获得链接字符串
string connStr = ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString();
con = new SqlConnection(connStr);
///获取Image控件物理路径
string[] PicName = IgMeet_Pic.ImageUrl.Split('/');
//PicName[PicName.Length -1]
if (this.FUp_Picture.HasFile)
{
//判断文件是否小于10Mb
if (this.FUp_Picture.PostedFile.ContentLength < 10485760)
{
try
{
获取图片大小
// fileDataStream = FUp_Picture.PostedFile.InputStream;
// System.Drawing.Image image = System.Drawing.Image.FromStream(fileDataStream);//从上传文件流中实例化Image类
// float w = image.Width;//获取上传文件的水平分辨率(以“像素/英寸”为单位)。
// float h = image.Height;//获取上传文件的垂直分辨率(以“像素/英寸”为单位)。
//图片 UU+格式
string[] GeShi = this.FUp_Picture.FileName.Split('.');
string UUSix = txt_UU.Text +"."+GeShi[GeShi.Length-1];
// string NowTime = DateTime.Now.ToFileTimeUtc().ToString() + this.FUp_Picture.FileName;
this.FUp_Picture.PostedFile.SaveAs(Server.MapPath("/Meet_PlcImages/" + UUSix));
if (File.Exists(Server.MapPath("/Meet_PlcImages/" + PicName[PicName.Length - 1])))//必须是物理路径
{
File.Delete(Server.MapPath("/Meet_PlcImages/" + PicName[PicName.Length - 1]));
}
/*注意->这里为什么不是:FileUpLoad1.PostedFile.FileName
* 而是:FileUpLoad1.FileName?
* 前者是获得客户端完整限定(客户端完整路径)名称 this.FUp_Picture.FileName
* 后者FileUpLoad1.FileName只获得文件名.*/
// hs.Add("LogUrl", "/Meet_PlcImages/" + UUSix);//加载数据库用的
}
catch
{
// hs.Add("LogUrl", "");
}
}
else
{
new ClassMessageBox(this).UIAlter("图片不得大于10M!", "", true);
}
}
}