Image Upload background implementation, Image Upload background implementation
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); // get the file name
Int j = fn. LastIndexOf (".");
String newexts = fn. Substring (j );
String pname = IDCard;
Newname = pname + newexts;
String fp = "../UpImageFiles/"; // actual server folder address
String fpsql = "~ /UpImageFiles/"; // folder address recorded in the database
Url = fp + newname; // Server File address
Urlsql = fpsql + newname; // file address recorded on the server
Var urldel = jurl + fod + newname; // the absolute path of the image.
If (System. IO. File. Exists (Server. MapPath (url )))
{
// MessageBox. Show (this, "the file already exists! ");
File. Delete (urldel );
}
Files. SaveAs (Server. MapPath (url); // upload
String path = Server. MapPath (fp );
// Obtain the index of the last "." In the file name (including the path)
Int I = fn. LastIndexOf (".");
// Get the file extension
String newext = fn. Substring (I );
String data = IDCard; // new name
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 ('failed to save! "+ En. Message +" '); </script> ";
// MessageBox. Show (this, "an error occurs. please upload it again! ");
}
}