var request = this.Context.Request;
HttpPostedFile files = request.Files["idFile"];
string newname = null;
if (files.ContentLength > 0) {
try
{
string url;
string urlsql;
var jurl = AppDomain.CurrentDomain.BaseDirectory;
var fod = "UpImageFiles/";
string fn = files.FileName.Substring(files.FileName.LastIndexOf("\\") + 1);//獲取文件名
int j = fn.LastIndexOf(".");
string newexts = fn.Substring(j);
string pname = IDCard;
newname = pname + newexts;
string fp = "../UpImageFiles/";//服務器實際文件夾地址
string fpsql = "~/UpImageFiles/";//數據庫中記錄的文件夾地址
url = fp + newname;//服務器文件地址
urlsql = fpsql + newname;//服務器中記錄的文件地址
var urldel = jurl + fod + newname;//圖片的絕對路徑
if (System.IO.File.Exists(Server.MapPath(url)))
{
//MessageBox.Show(this, "文件已存在!");
File.Delete(urldel);
}
files.SaveAs(Server.MapPath(url));//上傳
string path = Server.MapPath(fp);
//取得文件名(抱括路徑)裡最後一個"."的索引
int i = fn.LastIndexOf(".");
//取得文件擴展名
string newext = fn.Substring(i);
string data = IDCard;//新名稱
FileInfo fii = new FileInfo(path + fn);
urlsql = fpsql + data + newext;
string path1 = fp + data + newext;
FileInfo fi1 = new FileInfo(path1);
if (fi1.Exists)
{
fi1.Delete();
}
}
catch(Exception en)
{
resultStr = "<script> alert('保存失敗!" + en.Message + "');</script>";
// MessageBox.Show(this, "出現錯誤,請重新上傳!");
}
}