Upload and display of MVC 4 images, and upload of mvc Images
First, let's take a look at how to upload:
1. view
Upload page:
1: @using (Html.BeginForm("Create", "Achievement", FormMethod.Post, new { enctype = "multipart/form-data" }))
2: {
3: <div class="editor-label">
4: @Html.LabelFor(model => model.Pictures)
5: </div>
6: <div class="editor-field">
7: <div><input type="file" name="Image" /></div>
8: </div>
9: }
Here we needNote the parameters of the BeginForm method.
2. control
1: public ActionResult Create(Achivement achieve, HttpPostedFileBase image)
2: {
3: try
4: {
5:
6: if (image != null && image.ContentLength > 0)
7: {
8: string fileName = DateTime.Now.ToString("yyyyMMdd") + "-" + Path.GetFileName(image.FileName);
9: string filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
10: image.SaveAs(filePath);
11: achieve.Pictures = "~/Images/" + fileName ;
12: }
13: m_achivementService.Create(achieve);
14: return RedirectToAction("Index");
15: }
16: catch
17: {
18: return View();
19: }
20: }
Now the image has been uploaded to the Images directory,Note that the path of the image saved in the Pictures field must contain "~".
Now let's take a look at how to display:
1. view
1: @using (Html.BeginForm("Edit", "Achievement", FormMethod.Post, new { enctype = "multipart/form-data" }))
2: {
3: <div class="editor-label">
4: @Html.LabelFor(model => model.Pictures)
5: </div>
6: <div class="editor-field">
7: @*@Html.EditorFor(model => model.Pictures)
8: @Html.ValidationMessageFor(model => model.Pictures)*@
9: <div><input type="file" name="Image" /></div>
10: <div>
11: @if (string.IsNullOrEmpty(Model.Pictures))
12: {
13: @:None
14: }
15: else
16: {
17:
18: }
19: </div>
20: </div>
21: }
Note that Model. Pictures cannot be written directly in src.@ Url. ContentOtherwise, c:/images/123.pngis displayed. The image cannot be displayed normally.
control:
The same operation as create, Which is omitted here.
How does aspnet mvc4 upload photos in a table?
Hide a form at the bottom of the page, which is not displayed by default. Javascript is used to control display and upload. ajax can return a path after asynchronous upload is submitted.
How to upload and display images in spring mvc
You can use components to upload JspSmartUpload. This is a class.
<Form name = "f1" id = "f1" action = "/demo/servlet/UploadServlet" method = "post" enctype = "multipart/form-data">
<Table border = "0">
<Tr>
<Td> User name: </td>
<Td> <input type = "text" name = "username" id = "username"> </td>
</Tr>
<Tr>
<Td> password: </td>
<Td> <input type = "password" name = "password" id = "password"> </td>
</Tr>
<Tr>
<Td> photo: </td>
<Td> <input type = "file" name = "pic" id = "pic"> </td>
</Tr>
<Tr>
<Td> photo: </td>
<Td> <input type = "file" name = "pic2" id = "pic2"> </td>
</Tr>
<Tr>
<Td colspan = "2" align = "center"> <input type = "submit"> </td>
</Tr>
</Table>
</Form>
This is directly submitted to the servlet through a form. You need to configure the servlet in spring. (generally, you do not need to configure the servlet ).
In the preceding JSp page, the following is the code in servlet/action. You know how to use the spring framework (no servlet but action ).
Package com. demo. servlet;
Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. uti... the remaining full text>