asp. net MVC realize no component upload picture example introduction

  • 2020-06-12 08:49:08
  • OfStack

Example:
If I want to upload a picture to the server side: asp page
 
<form id="form1" runat="server" action="/bookIndex/fileUpLoad/( You're ready to deal with it  ActionResult)" method="post" enctype="multipart/form-data"> 
<input type="file" id="imageUpLoad" name="imageUpLoad"> 
<input type="button" value=" Click on the upload " onclick="UpLoad()"> 
.... 
</form> 

js code:
 
<script type="text/javascript"> 
function UpLoad() 
{ 
 If there are other values, check if they are null . 
form1.submit(); 
} 
<script> 

The background code
 
public ActionResult fileUpLoad(HttpPostedFileBase imageUpLoad( Here's the front page input Input box name keep 1 to )) 
{ 
string fileName = imageUpLoad.FileName; 
// The transformation takes the filename only and removes the path.  
if (fileName.LastIndexOf("\\") > -1) 
{ 
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); 
} 
// Save to the relative path.  
imageUpLoad.SaveAs(Server.MapPath("../../image/img/" + fileName)); 
// The following code is will   The path is saved to the database.  
string ImagePath = "../../image/img/" + fileName; 
string sql = "insert into bookinfo(bookphoto)values('" + ImagePath + "')"; 
// Wrapped code, call directly.  
DataBase db = new DataBase(); 
db.getConn(); 
int result = db.executeUpdate(sql); 
return View(); 
} 

Related articles: