Asp.net file and Image Upload

Source: Internet
Author: User
Add a <input id = "fileup" type = "file" runat = "server" name = "file1"> control on the ASPX page
A button control

In the button event of the Aspx. CS page String Filepath = "" , Fileextname = "" , Mfilename, mpath;
System. Text. stringbuilder strme
=   New System. Text. stringbuilder ();
If (Fileup. postedfile. filename ! = "" )
{
Filepath
= Fileup. postedfile. filename;
Fileextname
= Filepath. substring (filepath. lastindexof ( " . " ) + 1 );
Try
{
Mpath
= Server. mappath ( " Upfile " );
// Get File Name
Mfilename = Filepath. substring (filepath. lastindexof ( " \\ " ) + 1 );
Fileup. postedfile. saveas (mpath
+ " / " + Mfilename );
// You must add "/" in the middle. Otherwise, the upfile folder name will be used as a part of the name of the uploaded file.
}

Catch (Exception eeee)
{

}

}
 

If the image is uploaded to the server and stored in the database to retrieve the path on different pages, you can save the image name to the database and put the image into a directory.


string patht = request. applicationpath + " \ businesscard/ " + request. querystring [ " strr " ]. replace ( " ' " , "" );;
image1.imageurl
= patht;

request. applicationpath is the physical path of the server root directory
businesscard is the business card directory, request. querystring ["strr"]. replace ("'", ""); get the image name from the page and remove the ''added during the transfer process.

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.