<script type="text/javascript">
var i=1
function addFile()
{
if (i<8)
{var str = '<BR> <input type="file" name="File" runat="server" style="width: 300px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" />'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
else
{
alert("您一次最多只能上传8张图片!")
}
i++
}
function verCode()
{
alert("验证码输入错误");
}
</script>
<INPUT onclick="addFile()" type="button" value="增加图片(Add)"><br />
<input />
===========================================================
.cs 代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Data.SqlClient;
using System.Text;
protected void btnUpload_Click(object sender, EventArgs e)
{
if (Session["userid"] != null && Session["user"] != null && Session["model"] != null && Session["userid"].ToString() != "" && Session["user"].ToString() != "" && Session["model"].ToString() != "")
{
if (this.ddlAlbum.SelectedValue == "请选择产品的类别")
{
Jsp.Alert("请选择类别");
}
else
{
if (this.txtVerifyCode.Text.Trim().ToUpper() != Session["VerifyCode"].ToString())
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "define", "<script>verCode()</script>");//动态注册脚本
}
else
{
lblMessage.Text = "";
lblMessage.Visible = false;
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
string[] rd = Request.Form[1].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述
string albumid = ddlAlbum.SelectedValue.Trim();
int ifile;
for (ifile = 0; ifile < files.Count; ifile++)
{
if (files[ifile].FileName.Length > 0)
{
System.Web.HttpPostedFile postedfile = files[ifile];
string fex = Path.GetExtension(postedfile.FileName);
if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF")
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---图片格式不对,只能是jpg或gif<br>");
int num2 = ifile + 1;
Jsp.Alert("您选择的第" + num2 + "张图片错误!图片格式不正确只能是jpg或gif");
imgState = false; break;
}
if (postedfile.ContentLength > 200 * 1024)//单个文件不能大于200k
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于1024k<br>");
int num1 = ifile + 1;
Jsp.Alert("您选择的第" + num1 + "张图片错误!图片大小不能大于200K");
imgState = false; break;
}
System.Drawing.Image image = System.Drawing.Image.FromStream(files[ifile].InputStream);
int width = image.Width;
int height = image.Height;
int num = ifile + 1;
if (width > 300 || height > 500)
{
Jsp.Alert("您选择的第" + num + "张图片错误!您的图片是" + width + "*" + height + "!请您重新上传尺寸小于300*500的图片");
imgState = false; break;
}
}
}
if (strmsg.Length <= 0)//说明图片大小和格式都没问题
{
//以下为创建图库目录
//string dirname = "pic00" + ddlAlbum.SelectedValue.Trim();
string dirpath = Server.MapPath("..//..//dress_corp/ProIMG");
//dirpath = dirpath +"\\" + dirname;
if (Directory.Exists(dirpath) == false)
{
Directory.CreateDirectory(dirpath);
}
Random ro = new Random();
int name = 1;
if (imgState)
{
try
{
for (int i = 0; i < files.Count; i++)
{
System.Web.HttpPostedFile myFile = files[i];
string FileName = "";
string FileExtention = "";
string PicPath = "";
FileName = System.IO.Path.GetFileName(myFile.FileName);
string stro = ro.Next(100, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片
string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
{
FileExtention = System.IO.Path.GetExtension(myFile.FileName);
string ppath = dirpath + "\\" + NewName + FileExtention;
myFile.SaveAs(ppath);
string FJname = FileName;
PicPath = NewName + FileExtention;
}
AddPicture(PicPath, rd[i], albumid);//将图片信息保存到数据库
}
Jsp.GoBack("添加成功", "qy-fab-list.aspx");
}
catch
{
Jsp.Alert("添加失败");
}
}
}
else
{
lblMessage.Text = strmsg.ToString();
lblMessage.Visible = true;
}
}
}
}
else
{
Jsp.GoBack("连接超时,请重新登陆!", "/user/login.aspx");
}
}
public void AddPicture(string imgpath, string imgnote, string albumid)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into e_ProShowPhoto(");
strSql.Append("PhotoTitle,PhotoFileName,CateType,UserID)");
strSql.Append(" values (");
strSql.Append("@PhotoTitle,@PhotoFileName,@CateType,@UserID)");
SqlParameter[] parameters = {
new SqlParameter("@PhotoTitle", SqlDbType.VarChar,40),
new SqlParameter("@PhotoFileName", SqlDbType.VarChar,50),
new SqlParameter("@CateType", SqlDbType.VarChar,50),
new SqlParameter("@UserID", SqlDbType.Int,4)
};
parameters[0].Value = imgnote;
parameters[1].Value = imgpath;
parameters[2].Value = albumid;
parameters[3].Value = userID;
SqlHelper.ExecuteSql(strSql.ToString(), parameters);
}
==============================
<form id="form1" runat="server"><div>
图片上传:生成缩略图 加文字水印 图片水印<br />
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传图片" /><br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<br />
远程图片抓取(保存到本地)支持jpg、gif、bmp、png<br />
图片抓取后自动以日期+原名称命名<br />
输入远程图片地址(支持Html)<br />
<asp:TextBox ID="TextBox1" runat="server" Height="308px" TextMode="MultiLine" Width="584px"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="开始抓取" /></div>
</form>
using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12using System.Net;
13using System.Text.RegularExpressions;
14
15namespace ImgUpload
16 }
}
}
}
else
{
if (infotype == "今日新款")
{
guangGao = "<img src='/Wholesale%20Member/background/jrxk/PiFaImg/no.gif'></img>";
}
if (infotype == "特价商品")
{
guangGao = "<img src='/Wholesale%20Member/background/tjsp/PiFaImg/no.gif'></img>";
}
}
=============
public static string[] SetMoreInfo(string str,char chr)
{
if (str == String.Empty || str == null)
return null;
else
{
string[] alist = str.Split(chr);
return alist;
}
}
============
/// <summary>
/// 便利指定路径下的图片
/// </summary>
/// <param name="path">路径</param>
/// <param name="picName">图片名</param>
/// <returns>bool有的话true,没有的话就false</returns>
public static bool GetImgState(string path,string picName)
{
if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(path + picName)))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 判断图片是否存在 并且判断是否在指定范围内
/// </summary>
/// <param name="path"></param>
/// <param name="picName"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static bool GetImgStateBySize(string path, string picName, int width, int height)
{
if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(path + picName)))
{
System.Drawing.Image image = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(path + picName));
if (image.Width < width && image.Height < height)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}