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, "出现错误,请重新上传!"); } }