Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (4)--Upload Avatar

Source: Internet
Author: User
Tags mysql code

Write in front

Recently started busy, the schedule is tight compared to rush, but also tomorrow to go to the driving school, can only a function of a function of the added, perhaps each completion of the function does not count what, and so will be implemented, and then find a good point of the UI to the front-end refactoring.

Series Articles

[Ef]vs15+ef6+mysql Code First mode

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (1)

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (2)--User Registration

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (3)--Verification code

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (4)--Upload Avatar

Example

Here's how to upload files in the simplest form. Of course you can use plugins or something.

@using (Html.BeginForm ("Register","UserInfo", FormMethod.Post,New{enctype ="Multipart/form-data"}) {@Html. AntiForgeryToken ()<divclass="Form-horizontal"> @Html. ValidationSummary (true)        <divclass="Form-group">@Html. Labelfor (Model= Model. Header,New{@class ="Control-label col-md-2" })            <divclass="col-md-10"> <input type="file"accept="image/*"Name="name"Value=""/> </div> </div> <divclass="Form-group"> <labelclass="Control-label col-md-2"> Verification Code </label> <divclass="col-md-10"> <input type="text"Name="name"Value=" "/> "Imgcode"style="Cursor:pointer;"title="switch to the next one"Src="/userinfo/verifycodeimage"alt="Verification Code"/> </div> </div> <divclass="Form-group"> <divclass="col-md-offset-2 col-md-10"> <input type="Submit"Value="Create" class="btn Btn-default"/> </div> </div> </div>}<div>@Html. ActionLink ("Back to List","Users")</div>

Controller

[HttpPost] Publicactionresult Register (UserInfo UserInfo) {intSavecount =0; if(modelstate.isvalid) {varFiles =Request.Files; if(Files. Count >0)                {                    varFile = files[0]; stringStrfilesavepath = Request.mappath ("~/content/images"); stringStrfileextention =path.getextension (file.                    FileName); if(!directory.exists (Strfilesavepath))                    {directory.createdirectory (Strfilesavepath); } file. SaveAs (Strfilesavepath+"/"+ Userinfo.displayname +strfileextention); Userinfo.header="/content/images/"+ Userinfo.displayname +strfileextention;                } _userinfoservicerepository.add (UserInfo); Savecount=_userinfoservicerepository.savechanges (); }            if(Savecount >0)            {                returnRedirecttoaction ("Users"); }            Else            {                returnView (UserInfo); }        }

Results

Summarize

Using native submission forms is the simplest way, in fact, if the mobile phone browser does not support Flash or HTML5, this is the most appropriate way.

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (4)--Upload Avatar

Related Article

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.