How MVC uses Ajax post to upload pictures

Source: Internet
Author: User

Let's summarize how to upload a picture using Ajax as a post.

One, the common one is to request in file format. Gets the file in the Request.Files.

   Public ActionResult UploadFile (HttpContext request)        {            = request.files[0];             string @" upload/ " ;            Img. SaveAs (Path.Combine (Path, IMG). FileName));             return View ();        }

Second, the other is to upload the JSON 64-bit encoding (base64 URL) image data to save.

The 64-bit encoded picture data format is generally as follows:

data:image/png;base64,ivborw0kggoaaaansuheugaabg4aaah0cayaaabscgvoaaagaeleqvr4xuy9c5rv1zmo++ 96p3zvursv4heefdqobyktjesjjme+6wgtppuqirfpy8nrui/aklcvy2hon0fn5tiubh/6xds9bgah0+1by+ gk2p2qfpiagk9aigbhlqgiokgvrqfvu3bd9s+c21wbxvv71157pfb+lonh1ar5+oc3z/ H46p9zrnqsr3ggaaeiqaaceiaabcaaaqhaaaiqgaaeakcggrgj3jwqeaqgaaeiqaaceiaabcaaaqhaaaiqsakgblgieiaabcaaaqhaaaiqgaaeiaabceagoaq Qnwgdgmkcaaqgaaeiqaaceiaabcaaaqhaaakig9yabcaaaqhaaaiqgaaeiaabceaaahaikahetuanhragaaeiqaaceiaabcaaaqhaaaiqgadihjuaaqhaaaiq Gaaeiaabceaaahcaaaqcsgbxe9cjiswiqaaceiaabcaaaqhaaaiqgaaeiic4yq1aaaiqgaaeiaabceaaahcaaaqgaigaekdcbhricasceiaabcaaaqhaaaiqg Aaeiaabccbuwamqgaaeiaabceaaahcaaaqgaaeiqccgbba3az0ywoiabcaaaqhaaaiqgaaeiaabceaaaogb1gaeiaabceaaahcaaaqgaaeiqaaceagoacrnqc Egscaaaqhaaaiqgaaeiaabceaaahcaaokgnqabceaaahcaaaqgaaeiqaaceiaabajkahet0ikhlahaaaiqgaaeiaabceaaahcaaaqgglhhduaaahcaaaqgaae Iqaaceiaabcaagyasqnwedgiicwiqgaaeiaabceaaahcaaaqgaaeiig5yaxcaaaqgaaeiqaaceiaabcaaaqhaikaeedcbnrjcggaeiaabceaaahcaaaqgaaei Qaacibvwaaqgaaeiqaaceiaabcaaa... Agkkca4cbbouosaqiecbagqiaaaqiecbagkiaa4canouofaqiecbagqiaaaqiecbagkkca4cbbouosaqiecbagqiaaaqiecbagkiaa4canouofaqiecbagqia Aaqiecbagkkca4cbbouosaqiecbagqiaaaqiecbagkiaa4canouofaqiecbagqiaaaqiecbagkkca4cbbouosaqiecbagqiaaaqiecbagkiaa4canouofaqie cbagqiaaaqiecbagkkca4cbbours9wlly8vhypurob+/ Pxw8ebalaqiecbagqiaaaqiecbaonidgpnals7vby2nj4e7du6gpqskmdg4kxniut94riecaaaecbagqiecaqbkbwy0hujcc09pt4entp9kmmdixozsbhoehw /z8fojp6qldq0ohubk5xl16nbs3t4epqakwmjis2trafvtavxv3ws2bn1e+ pjo6gmzmzkjls8ua13r0dnsnh4yqiecaaaecbagqiecaaahbjtfqnwlj4+ph2lfjq86y+xzzu29vb4ghzphjx0nfx1/w/vj11v6lac+ fcxdcdhg6urqy82nandk5gs5evpija+ ogq0miecbagaabagqiecbagmanacgnyva3aqsfn4uli9msqbm5uzxluzg4kquyprkymbj50ms3btzilmcvjinhjmtbzq+ vqrsmdsfagaabagqiecbagaabaoiby6cebcymjrklugmdayvniq/ fmtjhjx4nly9fdpcuxqp5gpvpz9yubr5a2fnphtpcgaabagqiecbagaabagtmudeg6kqgtdlxbntp06fd4cohw7vr17j9au6dohwwlpbcmtnnvnrgtdwdp7th Twnj49ucolrc0bgcbagqiecaaaecbagqahgbwu3ddweabagqiecaaaecbagqiecaql0kcg7qttlaryaaaqiecbagqiaaaqiecds8wp8an7hudozwde4aaaaas Uvork5cyii= 

Can be assigned directly to SRC in the HTML image tag.

Ajax Upload Method:

var picture = jqplottoimg ($ (' #ChartContent '));     // prompt ("", picture); ' POST ',        ' ...      /demo/uploadfilejson ',        false,        data: {source:picture},          function  (data) {            //alert (data);         }    });

Receive in MVC, and Save:

  PublicActionResult Uploadfilejson (Jobject request) {stringImg64data = Request. value<string> ("Imgdata"); using(MemoryStream ms =NewMemoryStream (convert.frombase64string (img64data))) {                using(Bitmap img =NewBitmap (MS)) {img. Save ("Savingpath"+"imagename.jpg"); }            }            returnJson ("OK"); }

 

How MVC uses Ajax post to upload pictures

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.