Introduction to asp.net MVC implementation of image uploading without Components

Source: Internet
Author: User

Example:
If I want to upload an image to the server: asp page Copy codeThe Code is as follows: <form id = "form1" runat = "server" action = "/bookIndex/fileUpLoad/(the ActionResult you are going to process) "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 codeThe Code is as follows: <script type = "text/javascript">
Function UpLoad ()
{
If there are other values, determine whether it is null.
Form1.submit ();
}
<Script>

Background codeCopy codeThe Code is as follows: public ActionResult fileUpLoad (HttpPostedFileBase imageUpLoad (consistent with the name of the input box on the foreground page ))
{
String fileName = imageUpLoad. FileName;
// Convert to get only the file name and remove the path.
If (fileName. LastIndexOf ("\")>-1)
{
FileName = fileName. Substring (fileName. LastIndexOf ("\") + 1 );
}
// Save it to the relative path.
ImageUpLoad. SaveAs (Server. MapPath (".../../image/img/" + fileName ));
// The following code saves the path to the database.
String ImagePath = ".../../image/img/" + fileName;
String SQL = "insert into bookinfo (bookphoto) values ('" + ImagePath + "')";
// Encapsulate the code and call it directly.
DataBase db = new DataBase ();
Db. getConn ();
Int result = db.exe cuteUpdate (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.