Picture upload and preview function

This time I share the problems of image upload and preview encountered in my work

Because we use the weex framework for development (weex is an old and abandoned framework), we do not support some easy-to-use ui libraries such as El ui, which are popular and have rich functions. Therefore, we use the native input (type = file) when solving the problem.

solve the problem

weex's own eeui library did not find the module for file upload. You have to use native js. Although there will still be type warnings, the function can be completed.

 <input type="file" id="bankCard" @change="saveImg" />

The writing method of html module has not changed, and it is still the normal writing method. Borrowing the change event triggers the methods of saving and uploading pictures.

saveImg method defined by methods
saveImg(event) {
      let files = event.target.files[0];
      if (files === undefined) {
        console.log("No pictures selected");
      const _that = this;
      if (window.FileReader) {
        var reader = new FileReader();
        //Event triggered after listening for file reading
        reader.onloadend = function (e) {
          const result = e.target.result;
          const ele = e.target;
          _that.imgSrc[event.target.id] = result;

It should be noted that FileReader is used in this method, which is used to convert images into bs64 format file stream. I choose reader onloadend. After the event, save the file stream to the data of the data component. It's convenient to do picture preview and other desired follow-up functions. Attach the data format in data.

 data: () => ({
    imgSrc: {
      certificates: "",
      bankCard: "",
      otherCategories: "",

Once the file is received, we can start uploading. We tried the formData format here, and there will always be errors. Finally, we decided to use the native ajax to upload.

uploadImg(e) {
      var headers = {};
      var files = e.target.files[0];
      console.log("files", files);
      func.getUserToken((token) => {
        if (token.length > 0) {
          headers["token"] = token;
        headers["netNo"] = "wechat";
      // const  { action } = this;
      // this.uploading = true;
      // let parms = new FormData();
      // parms.append("file",files)
      let data = {
        file: files,
      //  data = files

      console.log("shangchaun", data);
      const options = {
        headers: headers,
          "Write the request address here",
        onProgress: (e) => {
          this.uploadPercent = e.percent;
        onSuccess: (res) => {
          console.log("success", res);
          console.log("Is there any at this time files", files);
          // this.handleUploadInfo(res.data)
        onError: (err) => {},

In Ajax (option), you can bring the request header, back-end interface address and some subsequent parameters.
After the upload is successful, you can use the uploaded image bs64 file stream just saved in data to preview the file.

                  :class="imgSrc.bankCard ? 'bankCardImgSame':''"

Put the corresponding img The corresponding attribute under SRC is bound to the corresponding picture preview position. Of course, I may encounter some other problems in the implementation process. For example, when I encounter the above elements, I will block the input below. My solution is to use the attribute penetration syntax of css3.

CSS3 pointer-events Allow mouse clicks to penetrate subsequent elements
CSS new property pointer-events: Literally, it is a mouse click event, and the values are auto and none. 
When used pointer-events:none,Indicates that it will not capture any clicks, but just let the event penetrate below it. The code is as follows:
    .overlay {
        pointer-events: none;
<div class="div1">
    <div id="overlay" class="overlay"></div>
//Browser support:
Can see canIUse Data on, currently supported IE11,Firefox3.6+,Chrome,Safari4+,Opera15+,Android,ios.·       

Undo: Ctrl/Command + Z
Redo: Ctrl/Command + Y
Bold: Ctrl/Command + B
Italic: Ctrl/Command + I
Title: Ctrl/Command + Shift + H
Unordered list: Ctrl/Command + Shift + U
Ordered list: Ctrl/Command + Shift + O
Checklist: Ctrl/Command + Shift + C
Insert code: Ctrl/Command + Shift + K
Insert link: Ctrl/Command + Shift + L
Insert picture: Ctrl/Command + Shift + G
Find: Ctrl/Command + F
Replace: Ctrl/Command + G

Creating a reasonable title is helpful to the generation of the directory

Directly input once #, and press space to generate level 1 title.
After entering twice #, and pressing space, a level 2 title will be generated.
By analogy, we support level 6 titles. It helps to generate a perfect directory after using TOC syntax.

How to change the style of text

Emphasize text emphasize text

Bold text bold text

Tag text

Delete text

Reference text

H2O is a liquid.

The result of 210 operation is 1024

Insert links and pictures

Link: link.


Pictures with dimensions:

Centered picture:

Centered and sized picture:

Of course, in order to make users more convenient, we have added the image drag function.

How to insert a beautiful piece of code

go Blog settings Page, select a code slice highlighting style you like, and the same highlighted code slice is shown below

// An highlighted block
var foo = 'bar';

Generate a list that suits you

  • project
    • project
      • project
  1. Item 1
  2. Item 2
  3. Item 3
  • Plan task
  • Complete the task

Create a table

A simple table is created as follows:

project Value
computer $1600
mobile phone $12
catheter $1

The setting content is centered, left and right

Use: ---------: Center
Use: --------- left
Usage -----------: right

First column Second column Third column
First column text centered The text in the second column is on the right The text in the third column is left


SmartyPants converts ASCII punctuation characters into "smart" printed punctuation HTML entities. For example:

Single backticks 'Isn't this fun?' 'Isn't this fun?'
Quotes "Isn't this fun?" "Isn't this fun?"
Dashes -- is en-dash, --- is em-dash – is en-dash, — is em-dash

Create a custom list

Markdown Text-to-HTML conversion tool Authors John Luke

How to create a footnote

A text with footnotes. 1

Comments are also essential

Markdown converts text to HTML.

KaTeX mathematical formula

You can render LaTeX mathematical expressions using KaTeX:

Gamma formula display Γ (n)=(n−1)! ∀n∈N\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ (n)=(n−1)! ∀ n ∈ n is through Euler integral

Γ(z)=∫0∞tz−1e−tdt . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=∫0∞​tz−1e−tdt.

You can find more information about LaTeX mathematical expressions here.

New Gantt chart function to enrich your articles

  • For Gantt chart syntax, refer to here,

UML diagram

UML diagrams can be used for rendering. Mermaid For example, a sequence diagram is generated as follows:

This will produce a flowchart.:

  • For Mermaid syntax, refer to here,


We will still support the flowchart of flowchart:

  • For Flowchart syntax, refer to here.

Export and import


If you want to try this editor, you can edit it at will in this article. When you have finished writing an article, find the article export in the upper toolbar and generate one md file or html file for local saving.


If you want to load an article you have written md file, you can select the import function in the upper toolbar to import the file with the corresponding extension,
Continue your creation.

  1. Explanation of footnotes ↩︎

Tags: Javascript Vue

Posted by nikefido on Tue, 24 May 2022 08:46:59 +0300