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