asp.net MVC implementation non-component upload Picture Example Introduction _ Practical Skills

Source: Internet
Author: User
Example:
If I want to upload a picture to the server side: ASP page
Copy Code code as follows:

<form id= "Form1" runat= "Server" action= "/bookindex/fileupload/" (you are prepared to process ActionResult) "method=" POST "enctype=" Multipart/form-data ">
<input type= "File" id= "Imageupload" name= "Imageupload" >
<input type= "button" value= "click to upload" onclick= "UpLoad ()" >
....
</form>

JS Code:
Copy Code code as follows:

<script type= "Text/javascript" >
function UpLoad ()
{
If there are other values, determine if the bottom is empty.
Form1.submit ();
}
<script>

Background code
Copy Code code as follows:

Public ActionResult FileUpload (HttpPostedFileBase imageupload (consistent with front page input box name)
{
string fileName = Imageupload.filename;
The conversion only gets the filename and removes the path.
if (Filename.lastindexof ("\") >-1)
{
FileName = filename.substring (Filename.lastindexof ("\") + 1);
}
Save to a relative path.
Imageupload.saveas (Server.MapPath) (". /.. /image/img/"+ fileName));
The following code is to save the path to the database.
String ImagePath = ".. /.. /image/img/"+ fileName;
String sql = "INSERT into BookInfo (Bookphoto) VALUES (' + ImagePath + ')";
Encapsulated code, called directly.
Database db = new database ();
Db.getconn ();
int result = db.executeupdate (SQL);
return View ();
}

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.