I recently saw a question in the Q & A, asking how to use ajax to upload images in MVC. Here is a demo for detailed explanation.
This demoCodeIt is very simple, that is, there is an upload image button on a page. After clicking it, a layer pops up. In this pop-up layer, upload the image and update the image address to the page. After obtaining the uploaded image address, you can perform other operations (such as inserting it into a text editor ).
Controller
Public Class Imagecontroller: controller { Public Actionresult index (){ Return View ();} [httppost] Public Jsonresult upload (httppostedfilebase upimg ){ String Filename =System. Io. Path. getfilename (upimg. filename ); String Filephysicalpath = server. mappath ( " ~ /Upload/ " + Filename ); String PIC = "" , Error = "" ; Try {Upimg. saveas (filephysicalpath); PIC = " /Upload/ " + Filename ;} Catch (Exception ex) {Error = Ex. Message ;} Return JSON ( New {PIC = PIC, error = Error });}}
Tip: Upload the file to the upload folder in the root directory of the website. modify or add the directory as needed.
View
Index. cshtml:
@ {Viewbag. Title = "Index"; layout = "~ /Views/shared/_ layout. cshtml ";} @ section headcss { < Style Type = "Text/CSS" > Form { Border : 1px solid # ccc ; Border-radius : 5px ; Padding : 10px ; Margin : 10px 0 ; Width : 400px ; Background : # Eee ; } </ Style > } @ Section headscript { < Script SRC = "/Scripts/jquery. Form. js" Type = "Text/JavaScript" > </ Script > < Script Type = "Text/JavaScript" > $ ( Function () {$ ( " # Btn_show " ). BIND ( " Click " , Function () {$ ( " # Form_upload " ). Show (); VaR Options = {Success: Function (Responsetext, statustext, xhr, $ form ){ VaR Picpath = Responsetext. PIC; If (Picpath = "" ) {Alert (responsetext. Error );} Else {$ ( " # Form_upload " ). Hide (); $ ( " # Result " ). ATTR ( " SRC " , Picpath). Show () ;}}, error: Function (XMLHttpRequest, textstatus, errorthrown) {console. Log (textstatus); console. Log (errorthrown) ;};$ ( " # Form_upload " ). Ajaxform (options );});}); </ Script > } < Input Type = "Button" ID = "Btn_show" Value = "Uploading images" /> < BR /> < Form ID = "Form_upload" Style = "Padding: 20px; display: none ;" Action = "Upload" Method = "Post" Enctype = "Multipart/form-Data" > < Input Name = "Upimg" Style = "Width: 350px; Height: 25px ;" Size = "38" Type = "File" /> < Input Type = "Submit" Value = "Upload" /> </ Form > < IMG ALT = "" Style = "Display: none ;" ID = "Result" SRC = "" />
Tip: You can obtain the uploaded image address in the success method of options. You can perform subsequent processing as needed.
_ Layout. cshtml:
<! Doctype html >
< Html >
< Head >
< Title > @ Viewbag. Title </ Title >
< Link Href = "@ URL. Content (" ~ /Content/site.css ")" rel = "Stylesheet" Type = "Text/CSS" />
@ Rendersection ("headcss", required: false)
< Script SRC = "@ URL. Content (" ~ /Scripts/jquery-1.4.4.min.js ")" Type = "Text/JavaScript" > </ Script >
@ Rendersection ("headscript", required: false)
</ Head >
< Body >
@ Renderbody ()
</ Body >
</ Html >
Several referenced files
Site.css and jquery-1.4.4.min.js don't talk about it, with vs create MVC project default has
Jquery. Form. JS, This Is A jquery form plug-in, address: http://jquery.malsup.com/form/
Effect
Open the page, click "Upload image" and select an image
After uploading
PS: This article is just a simple example. Many details are not handled. Please complete them as needed.