Show in img After selecting a local photo (Customer experience)

Source: Internet
Author: User
Tags readfile

Recently moved to the MVC project, and then again encountered the implementation of the photo upload, before the use of ASP, although there are photo uploads, and for the customer experience,

It also enables the selection of local photos immediately after the display in the IMG, here is a brief introduction to its implementation (ASP), the code is no longer written

  • Asp. NET implementation (using server control upload at that time):
    • An IMG control, a upload control
      • First create another new ASP. NET page, upload the photo, the photo upload displayed on this ASP, and then the IMG src link to this page O (method is very stupid, but just came out is so implemented, now can be implemented with plug-ins)
  • MVC implementation method (using file in input)
    • An IMG tag, a file tag
      • Implementation method
      •     //read the picture and display it to IMGfunction ReadFile () {varFile = This. files[0]; if(!/image\/\w+/. Test (File.type)) {Alert ("the file must be a picture! "); return false; }        varReader =NewFileReader ();        Reader.readasdataurl (file); Reader.onload=function (e) {$ ("#ComPic"). attr ("src", This. Result); }} $ (function () {varinput = document.getElementById ("Filetoupload"); //first determine if the browser supports FileReader//The label of the selected photo is disabled when the browser is not supported        if(typeofFileReader = = ='undefined') {alert ("Sorry, your browser does not support FileReader"); Input.setattribute ('Disabled','Disabled'); } Else{Input.addeventlistener (' Change', ReadFile,false); }    });
      • Here, you can display the image after selecting it, but it is important to note that the following version of IE6 (including IE6) is not supported for FileReader
      • then the image is uploaded (here, I'm using an asynchronous upload operation, and the database is saved in the picture path, and the picture is uploaded to the specified folder)
        varFileobj = document.getElementById ("Filetoupload"). files;varFilecontroller = "/member/shop/uploadimg" + "? Picname= "+ picname +" &pictype= "+Pictype;varform =NewFormData (); for(vari = 0; i < fileobj.length; i++) Form.append ("File" +I, fileobj[i]); varXHR =NewXMLHttpRequest (); Xhr.open ("Post", Filecontroller,true); Xhr.onload=function () {              if(Xhr.status = = && Xhr.responsetext = = "1") {alert ("Photo Upload is successful!"); } Else {
        The message returned when the picture was uploaded unexpectedly alert (xhr.responsetext); } }; Xhr.send (form);

  • There is another way to achieve, is to use uploadify, you can achieve bulk upload pictures or files, including the progress bar, the plug-in official document has been described in detail, in this no more said.

Show in img After selecting a local photo (Customer experience)

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.