Example code of the thinkjs file upload function,

Source: Internet
Author: User

Example code of the thinkjs file upload function,

Introduction

ThinkJS is a Node. js framework for future development. It integrates a large number of project best practices to make enterprise-level development so simple and efficient. Starting from 3.0, the underlying framework is implemented based on Koa 2.x and is compatible with all features of Koa.

Features

  • Compatible with middleware based on Koa 2.x
  • The kernel is small and supports plug-ins such as Extend and Adapter.
  • Excellent performance, high coverage of unit test
  • Built-in automatic compilation and update mechanisms for quick development
  • Use a more elegant async/await to handle asynchronous issues, and no longer support */yield
  • Support for TypeScript from 3.2

Architecture

Thinkjs is a node. js-based mvc framework developed by 360. This article uses thinkjs to write backend code. The specific code is as follows:

Let formdata = new FormData ($ ('form') [0]); $ ('# fileUpload '). submit (function (e) {e. preventDefault (); var data = new FormData ($ (this) [0]); $. ajax ({url: '/themestore/upload/theme', type: 'post', data: data, async: false, // send a synchronous request (TRUE by default) cache: false, // load request information in different browser caches (true by default) contentType: false, // The content encoding format processData: false,/* must be a Boolean parameter, the default value is true. By default, the sent data will be converted to an object (technically not a string) to work with the default content type "application/x-www-form-urlencoded ". If you want to send DOM tree information or other information that does not want to be converted, set it to false. */Success: function (res) {if (! Res. errno) alert ('upload successful! '); Else alert ('upload failed. Please try again! ');}});});

Note: async: false, cache: false, contentType: false, processData: false, which must be written.

In addition, you must use its own get method to view the values corresponding to the form items in the formdata object.

Eg: data. get ('file') // The get parameter is the name of the form element.

Async themeAction () {let themefile = this. file ('themename'); let filepath = themefile. path; // to prevent files uploaded with the same name from being overwritten due to repeated file names, path is a random name generated in MD5 mode. let uploadpath = think. RESOURCE_PATH + '/static/theme'; think. mkdir (uploadpath); // create this directory // extract the last part of the path separated. // Let basename = path. basename (filepath); let basename = themefile. originalFilename; // because the system does not allow you to upload a topic of the same name, you can directly use the topic name as the file name. // upload the file (the file path is filepath) move to the path of the second parameter and change it to the file name of the second parameter. Fs. renameSync (filepath, uploadpath + '/' + basename); themefile. path = uploadpath + '/' + basename; // read compressed file information stored in the database let zip = new JSZip (); this. success (themefile );}

Summary

The above is the example code of the thinkjs file upload function introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.