在aspx頁面添加一個<INPUT id="fileUp" type="file" runat="server" name="file1">控制項
一個Button控制項
在aspx.cs頁面的Button按鈕事件中string filePath="",fileExtName="",mFileName,mPath;
System.Text.StringBuilder strMe = new System.Text.StringBuilder();
if(fileUp.PostedFile.FileName!="")
{
filePath=fileUp.PostedFile.FileName;
fileExtName=filePath.Substring(filePath.LastIndexOf(".")+1);
try
{
mPath=Server.MapPath("UpFile");
//取得檔案名稱
mFileName=filePath.Substring (filePath.LastIndexOf("\\")+1);
fileUp.PostedFile.SaveAs(mPath+"/"+mFileName);
//中間一定要加"/",不然會把UpFile檔案夾名當成你上傳檔案 名的一部份
}
catch(Exception eeee)
{
}
}
如果圖片上傳到伺服器並存到資料庫中在不同的頁面中取路徑,則可只將 圖片名存入資料庫,圖片放入某一目錄。
然後另外的頁面中取則可為
string patht=Request.ApplicationPath+"\\BusinessCard/"+Request.QueryString["strr"].Replace("'","");;
Image1.ImageUrl=patht;
Request.ApplicationPath為伺服器根目錄的實體路徑
BusinessCard為名片目錄, Request.QueryString["strr"].Replace("'","");為取得頁面傳過來的圖片名並去掉傳送過程中添加的'';