//把相對路徑變成絕對路徑。
string absolutePath = Server.MapPath(relativePath);數據庫
FileUpload控件:
屬性:
FileName:文件名
HasFile:bool 是否選中了文件
FileBytes:要上傳文件的二進制數據
方法:
SaveAs(string 絕對路徑):上傳,另存爲。ide
1、上傳到硬盤文件夾
(一)傳單個文件
第一步:準備好文件及路徑:
//把以前在客戶端的文件名給取出來
string fileName = FileUpload1.FileName;字體
//防止文件重名
fileName = DateTime.Now.ToString("yyyyMMddhhmmsss") + fileName;spa
//把相對路徑轉化爲絕對路徑
string path = Server.MapPath("uploads/" + fileName);code
第二步:執行上傳:
//上傳
FileUpload1.SaveAs(path); //參數必須根路徑
注意:
1.如何防止文件重名?
2.如何防止同一時間點不一樣用戶傳統一文件名?orm
事例1:上傳單個文件對象
須要的控件FileUpload,buttonblog
1 protected void btnUpload_Click(object sender, EventArgs e) 2 { 3 //FileUpload1.SaveAs(@"E:\0128授課內容\0530\uploads\aaa.jpg"); 4 //把以前在客戶端的文件名給取出來 5 string fileName = FileUpload1.FileName; 6 7 //防止文件重名 8 fileName = DateTime.Now.ToString("yyyyMMddhhmmsss") + fileName; 9 10 //把相對路徑轉化爲絕對路徑 11 string path = Server.MapPath("uploads/" + fileName); 12 13 //上傳 14 FileUpload1.SaveAs(path); //參數必須根路徑 15 16 }
(二)傳多個文件:
思路:遍歷表單中全部的FileUpload控件,若是選擇文件就上傳圖片
int index = 0;
foreach (Control ctrl in form1.Controls)
{
if (ctrl is FileUpload)
{
index++;
//取得每一個上傳控件
FileUpload upload = ctrl as FileUpload;
//上傳控件中選上文件了
if (upload.HasFile)
{
//作文件路徑出來
string path = Server.MapPath("uploads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + index.ToString("00") + upload.FileName);string
//上傳
upload.SaveAs(path);
}
}
}
事例2:
須要的控件FileUpload,button
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class Default2 : System.Web.UI.Page 9 { 10 protected void Page_Load(object sender, EventArgs e) 11 { 12 13 } 14 protected void Button1_Click(object sender, EventArgs e) 15 { 16 int index = 0; 17 foreach (Control ctrl in form1.Controls) 18 { 19 if (ctrl is FileUpload) 20 { 21 index++; 22 //取得每一個上傳控件 23 FileUpload upload = ctrl as FileUpload; 24 //上傳控件中選上文件了 25 if (upload.HasFile) 26 { 27 //作文件路徑出來 28 string path = Server.MapPath("uploads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + index.ToString("00") + upload.FileName); 29 30 //上傳 31 upload.SaveAs(path); 32 } 33 } 34 } 35 } 36 }
(一)傳到數據庫去
1.作數據庫的操做代碼。DA Data
Image字段對應在程序裏是byte[]類型
2.作界面上的代碼。
a.把界面的值取出來
FileUpload1.FileBytes - 用來得到上傳文件的二進制數據。
b.送到數據庫去
(二)從數據庫中找出來,顯示出來
法一:會生成垃圾文件
在服務端生成一個JPG,把這個JPG的路徑賦給Image控件
法二:單獨作一個用來顯示圖片二進制數據的頁面。把這個頁面賦給Image控件。
事例3:
須要的控件FileUpload,button,textBox,image
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Data; 8 using DA; 9 using System.IO; 10 public partial class Default3 : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 15 } 16 protected void Button1_Click(object sender, EventArgs e) 17 { 18 //取界面數據 19 PhotoData data = new PhotoData(); 20 data.Name = FileUpload1.FileName; 21 data.Pic = FileUpload1.FileBytes; //FileBytes保存的是圖片的二進制數據 22 //送到數據庫去 23 new PhotoDA().Insert(data); 24 } 25 protected void Button2_Click(object sender, EventArgs e) 26 { 27 //從數據庫中把數據取出來 28 PhotoDA da = new PhotoDA(); 29 PhotoData data = da.Select(Convert.ToInt32(TextBox1.Text)); 30 31 string temp = Server.MapPath("temp.jpg"); 32 FileStream fs = new FileStream(temp, FileMode.Create); 33 fs.Write(data.Pic, 0, data.Pic.Length); 34 fs.Close(); 35 36 Image1.ImageUrl = "temp.jpg"; 37 } 38 protected void Button3_Click(object sender, EventArgs e) 39 { 40 Image1.ImageUrl = "ShowPIC.aspx?id=" + TextBox1.Text; 41 } 42 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Data; 8 using DA; 9 public partial class ShowPIC : System.Web.UI.Page 10 { 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 string id = Request["id"].ToString(); 14 15 PhotoData data = new PhotoDA().Select(Convert.ToInt32(id)); 16 if (data != null) 17 { 18 byte[] buffer = data.Pic; 19 Response.OutputStream.Write(buffer, 0, buffer.Length); 20 Response.End(); 21 } 22 } 23 }
上傳圖片加水印:
//1、從上傳數據中,轉化成圖片對象。
Stream s = FileUpload1.FileContent;
System.Drawing.Image img = System.Drawing.Image.FromStream(s);
//2、對圖片對象進行畫水印
//1.造筆
SolidBrush brush = new SolidBrush(Color.Yellow);
//2.造字體
Font font = new Font("Comic Sans MS", 18);
//3.找到圖像繪圖區域
Graphics g = Graphics.FromImage(img);
g.DrawString("http://www.itNBA.com", font, brush, 0, 0);
//3、圖片對象另存到硬盤上去
string fileName = FileUpload1.FileName;
string path = Server.MapPath("uploads/" + fileName);
img.Save(path);
須要的控件FileUpload,button
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Drawing; 8 using System.IO; 9 public partial class Default4 : System.Web.UI.Page 10 { 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 14 } 15 protected void Button1_Click(object sender, EventArgs e) 16 { 17 //1、從上傳數據中,轉化成圖片對象。 18 Stream s = FileUpload1.FileContent; 19 System.Drawing.Image img = System.Drawing.Image.FromStream(s); 20 21 //2、對圖片對象進行畫水印 22 //1.造筆 23 SolidBrush brush = new SolidBrush(Color.Yellow); 24 //2.造字體 25 Font font = new Font("Comic Sans MS", 18); 26 //3.找到圖像繪圖區域 27 Graphics g = Graphics.FromImage(img); 28 //4.肯定開始畫位置 29 SizeF size = g.MeasureString("http://www.itNBA.com",font); 30 float x = 0, y = 0; 31 y = img.Height - size.Height; 32 x = img.Width - size.Width; 33 //5.開始畫 34 g.DrawString("http://www.itNBA.com", font, brush, x, y); 35 36 //3、圖片對象另存到硬盤上去 37 string fileName = FileUpload1.FileName; 38 string path = Server.MapPath("uploads/" + fileName); 39 img.Save(path); 40 } 41 }