Reference: http://www.cnblogs.com/luotaoyeah/p/3321070.html
1. Download uploadify components, copy to content folder
<link href="~/content/uploadify/uploadify.css" rel="stylesheet " /><script src="~/content/uploadify/jquery.uploadify.js"></ Script>
2. View
<script type="Text/javascript">$ (function () {$ ('#btn_upload'). Uploadify ({uploader:'/home/upload',//Server processing AddressSwf:'/content/uploadify/uploadify.swf', ButtonText:"Uploading Files",//button TextHeight the,//button HeightWidth the,//button WidthFiletypeexts:"*.jpg;*.png;",//types of files allowedFiletypedesc:"Please select a picture file",//Document DescriptionFormData: {"Imgtype":"Normal"},//parameters submitted to the server sideOnuploadsuccess:function (file, data, response) {//Response event Handling after a successful upload of a file vardata =$.parsejson (data); $("#photo"). attr ("src", Data.imgpath); //alert (data.imgpath); } });});</script><span id="Btn_upload"></SPAN><BR/>"Photo"Src="Http://www.yxweb.com.cn/images/upphoto.gif"alt="Please upload your work photo"/>
3. Controller
Publicactionresult Upload (httppostedfilebase Filedata) {//no file upload, direct return if(Filedata = =NULL||string. IsNullOrEmpty (filedata.filename) | | Filedata.contentlength = =0) { returnHttpnotfound (); } //Get file Full filename (contains absolute path)//file Storage path format:/files/upload/{date}/{md5}. {suffix name}//Example:/files/upload/20130913/43ca215d947f8c1f1ddfced383c4d706.jpg stringFileMD5 =GetStreamMD5 (Filedata.inputstream); stringFileeextension =path.getextension (filedata.filename); stringUploaddate = DateTime.Now.ToString ("YYYYMMDD"); stringImgtype = request["Imgtype"]; stringVirtualPath ="/"; if(Imgtype = ="Normal") {virtualpath=string. Format ("~/files/upload/{0}/{1}{2}", Uploaddate, fileMD5, fileeextension); } Else{virtualpath=string. Format ("~/files/upload2/{0}/{1}{2}", Uploaddate, fileMD5, fileeextension); } stringFullfilename = This. Server.MapPath (virtualpath); //Create a folder, save a file stringPath =Path.getdirectoryname (fullfilename); Directory.CreateDirectory (path); if(!System.IO.File.Exists (Fullfilename)) {Filedata.saveas (fullfilename); } vardata =New{Imgtype = Imgtype, Imgpath = Virtualpath.remove (0,1) }; returnJson (data, jsonrequestbehavior.allowget); } /// <summary> ///calculate the MD5 value of a file stream/// </summary> /// <param name= "Stream" >file input stream</param> /// <returns></returns> Public StaticString GetStreamMD5 (Stream stream) {MD5 Md5hasher=NewMD5CryptoServiceProvider (); /*computes the hash value of the specified Stream object*/ byte[] Arrbythashvalue =Md5hasher.computehash (stream); /*A string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value; for example, "f-2c-4a"*/ stringStrhashdata = System.BitConverter.ToString (arrbythashvalue). Replace ("-",""); returnStrhashdata; }
"What bike?" ASP. MVC4 Note 02: Uploading Files Uploadify component use