"What bike?" ASP. MVC4 Note 02: Uploading Files Uploadify component use

Source: Internet
Author: User

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"&GT;&LT;/SPAN&GT;&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.