Upload and delete images

Source: Internet
Author: User

1. upload images
Public String picupload ()
{

If (this. fileuploadpic. hasfile) // check whether a file exists
{
String fullfilename = This. fileuploadpic. postedfile. filename; // file path name
String filename = fullfilename. substring (fullfilename. lastindexof ("\") + 1); // image name
String type = fullfilename. substring (fullfilename. lastindexof (".") + 1); // image format

If (type = "jpg" | type = "jpg" | type = "GIF" | type = "GIF" | type = "BMP" | type = "BMP ") // determine whether the image is of the image type
{
If (this. fileuploadpic. postedfile. contentlength> 20*1024)
{
Response. Write ("<SCRIPT> alert ('upload image must be less than 20 KB! '); </SCRIPT> ");

}
Else
{
String Path = httpcontext. Current. Request. mappath ("~ /Admin/images/solutionpic/"); // obtain the path of the uploaded file's website directory
This. fileuploadpic. saveas (path + filename); // store the file to the disk
Return "~ /Admin/images/solutionpic/"+ filename;

// Response. Write ("<SCRIPT> alert ('image uploaded successfully! '); </SCRIPT> "); // prompt

// This. imgpicture. src = "~ /Admin/images/solutionpic/"+ filename; // display the image
}

}
Else
{
Response. Write ("<SCRIPT> alert ('non-image type, not allowed to upload! '); </SCRIPT> ");
}
}
Else
{
Response. Write ("<SCRIPT> alert ('file must be specified! '); </SCRIPT> ");
}
Return "";
}

Ii. Deleting Images

The solution is as follows:

1. When uploading a file, you will save the file to a folder on the website, and you will save the records for uploading the file to the database. We recommend that you add a field that records the path of the file in the database table. The data added by this field is the path of the file you are saving the upload (preferably relative path, rather than absolute path ).

2. when deleting data, read the file path stored in this record, and then use file. delete (server. mappath (File URL) method to delete the file, delete the database record. In this way, you can.

Public static void executesqltran ()
{
Using (oledbconnection conn = new oledbconnection (connectionstring) // connection string
{
Conn. open ();
Oledbcommand cmd = new oledbcommand ();
Cmd. Connection = conn;
Oledbtransaction Tx = conn. begintransaction ();
Cmd. Transaction = TX;
Int I = 0;
Try
{

Cmd. commandtext = "delete from XXX Where..."; // SQL statement
I = cmd. executenonquery ();
If (I> 0)
{
////////////////
Try
{
File. Delete (server. mappath (File URL ));
TX. Commit ();
}
Catch
{
TX. rollback ();
}
//// // Try catch for this part.
}
Else
{
TX. rollback ();
}
}
Catch (system. Data. oledb. oledbexception E)
{
TX. rollback ();
Throw new exception (E. Message );
}

}
}

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.