Image Upload background implementation, Image Upload background implementation

Source: Internet
Author: User

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! ");
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.