前臺(tái)代碼----》
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;
public partial class FileUp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
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(',');//獲得圖片描述的文本框字符串?dāng)?shù)組,為對應(yīng)的圖片的描述
//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];
if (postedfile.ContentLength / 1024 > 1024)//單個(gè)文件不能大于1024k
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于1024k<br>");
break;
}
string fex = Path.GetExtension(postedfile.FileName);
if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF"&& fex !=".png"&& fex !=".PNG")
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---圖片格式不對,只能是jpg或gif或png<br>");
break;
}
}
}
if (strmsg.Length <= 0) //說明圖片大小和格式都沒問題
{
//以下為創(chuàng)建圖庫目錄
string dirpath = Server.MapPath("photo");
if (Directory.Exists(dirpath) == false)
{
Directory.CreateDirectory(dirpath);
}
Random ro = new Random();
int name = 1;
for (int i = 0; i < files.Count; i++)
{
System.Web.HttpPostedFile myFile = files[i];
string FileName = "";
string FileExtention = "";
FileName = System.IO.Path.GetFileName(myFile.FileName);
string stro = ro.Next(100, 100000000).ToString() + name.ToString();//產(chǎn)生一個(gè)隨機(jī)數(shù)用于新命名的圖片
string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
if (FileName.Length > 0)//有文件才執(zhí)行上傳操作再保存到數(shù)據(jù)庫
{
FileExtention = System.IO.Path.GetExtension(myFile.FileName);
string ppath = dirpath + @"/" + NewName + FileExtention;
myFile.SaveAs(ppath);
}
name = name + 1;//用來重命名規(guī)則的變量
}
Response.Write("<script>alert('恭喜,圖片上傳成功!')</script>");
}
else
{
lblMessage.Text = strmsg.ToString();
lblMessage.Visible = true;
}
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。