Uploadify using the method case in MVC (above flyer picture)

Source: Internet
Author: User

In the View view:

<link href= "/scripts/uploadify-v3.2.1/uploadify.css" rel= "stylesheet" type= "Text/css"/><script src= "/ Scripts/jquery-1.7.1.min.js "type=" Text/javascript "></script><script src="/Scripts/uploadify-v3.2.1/ Jquery.uploadify.js "type=" Text/javascript "></script><script src="/scripts/uploadify-v3.2.1/ Swfobject.js "></script><script type=" Text/javascript "> $ (document). Ready (function () {$ (' #file_u Pload '). Uploadify ({' ButtonText ': ' Select Upload file ', ' Queueid ': ' Filequeue ',//specify where the upload progress bar shows ' SWF ':            ' @Url. Content ("~/scripts/uploadify-v3.2.1/uploadify.swf?ver= ' +math.random () + '") ', ' uploader ': '/home/upload ', ' removecompleted ': true, ' checkexisting ': true, ' filetypedesc ': ' uploaded file Type ', ' fil Etypeexts ': ' *.jpg;*.png;*.gif ', ' filesizelimit ': ' 1024kb ', ' auto ': false, ' multi ': false , ' Queuesizelimit ': 1,//number of uploaded files in a queueLimit ' uploadlimit ': 1,//maximum number of uploads allowed ' height ': +, ' width ': 80, ' Onuploadsuccess ': function (file, data, response) {var obj = Jquery.parsejson (data);//Turn the returned JSON sequence To the Obj object if (obj. Success) {$ (' #input '). val (obj.                    FilePath); $ (' #upsucc '). Text (' Upload successful!                ‘); } else alert (obj.            Message);    }        }); });

  

<tr> <th> @Html. Labelfor (Model=>model. URL) </th> <td> <input type="file" class="File_upload"Id="File_upload"/> <div id="Filequeue"></div> <a href="javascript:$ (' #file_upload '). Uploadify (' upload ');"> Upload </a><span id="UPSUCC"style="color:red"></span>@Html. textboxfor (M= M.url,New{id="input", @style ="Display:none"})                </> </tr>

Controller in

[HttpPost] Publicjsonresult Upload (httppostedfilebase fileData) {if(FileData! =NULL)            {                Try                {                    //save path after file upload                    stringFilePath = Server.MapPath ("~/uploads/"); if(!directory.exists (FilePath))                    {directory.createdirectory (FilePath); }                    stringFileName = Path.getfilename (filedata.filename);//Original file name                    stringFileExtension = Path.getextension (fileName);//file name extension                    stringSavename = Guid.NewGuid (). ToString () + fileextension;//Save file name                    intFileSize = filedata.contentlength/1024x768; if(FileSize >1024x768|| FileSize <=2|| (FileExtension! =". jpg"&& FileExtension! =". PNG"&& FileExtension! =". gif"))                    {                        returnJson (New{Success =false, Message ="upload failed! \ r Please upload jpg/png format picture, file size not more than 1MB"}, Jsonrequestbehavior.allowget); }                    Else{filedata.saveas (FilePath+savename); returnJson (New{Success =true, FilePath ="/uploads/"+savename}); }                }                Catch(Exception ex) {returnJson (New{Success =false, Message =Ex.                Message}, Jsonrequestbehavior.allowget); }            }            Else            {                returnJson (New{Success =false, Message ="Please select a file to upload! "}, Jsonrequestbehavior.allowget); }        }

HTTP 302 error encountered in background app This is because the plugin is not in the process of the session so public Jsonresult Upload (HttpPostedFileBase fileData) It's best to put it in a controller that doesn't have access rights!

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.