62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadModifyImg.aspx.cs" Inherits="register_UploadModifyImg" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function upload() {
var button = document.getElementById("Button1");
button.click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td id="myimg" style="text-align: center; width: 113px;">
<img alt="图片不能大于512K" id="photoshow" src="../images/photo.gif" width="113" height="129"
runat="server" style="padding-top: 0px" />
</td>
</tr>
<tr>
<td>
<input id="Photo" type="file" onchange="upload()" runat="server" style="width: 112px" />
</td>
</tr>
</table>
<span id="photo_span" style="color: Red;"></span>
<asp:Button ID="Button1" runat="server" Style="display: none" Text="Button" OnClick="Button1_Click" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class register_UploadImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files[0];
if (!isAllowdPictureType(file))
{
this.ClientScript.RegisterStartupScript(this.GetType(), string.Empty, "alert('上传文件格式不正确!');", true);
return;
}
else if (file.ContentLength > 500000)
{
this.ClientScript.RegisterStartupScript(this.GetType(), string.Empty, "alert('上传文件大小超过500K!');", true);
return;
}
else
{
string tempPicNum = Utility.CommonUtility.MakeGuid() + ".jpg";
Photo.PostedFile.SaveAs(Server.MapPath("temp/" + tempPicNum));
photoshow.Src = "../register/temp/" + tempPicNum;
Session["pic"] = file;
}
}
private bool isAllowdPictureType(HttpPostedFile file)
{
if (string.IsNullOrEmpty(file.FileName))
{
return true;
}
string suffix = file.FileName.Substring(file.FileName.LastIndexOf('.') + 1).ToLower();
string[] allowedSuffix = { "jpg", "bmp", "png", "jpeg", "gif" };
bool allowed = false;
foreach (string item in allowedSuffix)
{
if (object.Equals(item, suffix))
{
allowed = true;
break;
}
}
return allowed;
}
}
cmd = New SqlCommand("INSERT INTO 用户表(原图,缩略图) VALUES('" & imgsavepath.Trim & "','" & imgsltpath.trim & "')", conn)
cmd.ExecuteNonQuery()
其他字段自己加
'----------------------上传图片
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
'--------------------数据库连接
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.Data.SqlClient
Imports System.Math
Dim imgsavepath As String '----------原图片路径
Dim imgsltpath As String '----------缩略图片路径
Dim sss As String = "**"
‘上传图片并生成缩略图保存到文件夹 ,经过试验 是成功的 不过是VB的代码
Dim image, newimage As System.Drawing.Image
Dim width, height, newwidth, newheight As Integer
Dim callb As System.Drawing.Image.GetThumbnailImageAbort
If imgsavepath <> "" Then System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(imgsavepath))
Dim uploadname As String = inputfile.Value '获取待上传图片的完整路径,包括文件名
Dim imagename As String = "" '上传后的图片名,以当前时间为文件名,确保文件名没有重复
' Dim filetext As String = LCase(Right(Trim(inputfile.Value), 3))
' Dim filetext1 As String = LCase(Right(Trim(inputfile.Value), 4))
If inputfile.Value <> "" Then
Dim stuff As String = uploadname.Substring(uploadname.LastIndexOf(".") + 1) '获得上传的图片的后缀名
If stuff = "gif" Or stuff = "jpg" Or stuff = "jpeg" Or stuff = "bmp" Then
imagename = DateTime.Now.Ticks.ToString + "." + stuff
inputfile.PostedFile.SaveAs(Server.MapPath("userimages/") + imagename)
imgsavepath = Server.MapPath("userimages/") + imagename
'--缩略图
image = System.Drawing.Image.FromFile(imgsavepath)
width = image.Width
height = image.Height
If width > height Then
newwidth = 125
newheight = image.Height / image.Width * newwidth
Else
newheight = 104
newwidth = image.Width / image.Height * newheight
End If
newimage = image.GetThumbnailImage(newwidth, newheight, callb, New System.IntPtr())
newimage.Save(Server.MapPath("userimages/suolvetu/" + imagename))
imgsltpath = "userimages/suolvetu/" + imagename
image.Dispose()
'-------------------------
imgtou.ImageUrl = imgsltpath 'Server.MapPath("userimages/suolvetu/" + imagename)
response.write sss & "成功上传图片"
Response.Write(imgsavepath)
Response.Write("<br/>")
Response.Write(inputfile.Value)
Else
response.write sss & "请选择正确格式的图片"
Exit Sub
End If
Else
response.write sss & "请选择上传的图片"
Exit Sub
End If
<asp:Image ID="imgtou" Height="104px" Width="100px" runat ="server" ImageAlign ="AbsMiddle" ImageUrl ="~/sysimage/01.gif" />
<input id="inputfile" type ="file" runat ="server" style="width: 208px" />
<asp:Button ID="shangch" runat="server" Text="上传" Width="39px" Enabled="False" />