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