Hbuilder Development App Combat-age 03-File Upload

Source: Internet
Author: User

Objective

The problem with doing apps is uploading files. Using Hbuilder to develop apps makes uploading very easy.


Uploader

The uploader module manages network upload tasks for uploading various files from the local to the server and supporting cross-domain access operations.

Upload Management objects can be obtained through Plus.uploader.


Uploader uploads the data using the Post method of HTTP, the data format conforms to the MULTIPART/FORM-DATA specification. The rfc1867 (form-based File Upload in HTML) protocol.

A lot of other content: Uploader


Code
Qiao.h.upload = function (options) {    if (!options) return;        var url = options.url;    var filepath = Options.filepath;    var datas = Options.datas | | [];    var success = options.success;    var fail = Options.fail;    if (URL && filepath) {        var task = plus.uploader.createUpload (URL, {                method: "POST",                blocksize: 204800,                priority:100            },            function (t, status) {                if (status = =) {                    if (success) success (t);                } else{                    if (fail) fail (status);                }        );        Task.addfile (filepath, {key: ' file '});        if (datas && datas.length) {for            (var i=0; i<datas.length; i++) {                var data = datas[i];                Task.adddata (Data.key, Data.value);            }        }        Task.start ();    }};

The upload of uploader was encapsulated,

Being able to see its core is a createupload method that creates a uploader object,

When this object is created, a successful and failed callback function is written.

Add data to this upload object after the creation is successful. Contains the files and other data to be uploaded,

Finally, run the Start method to start the upload.


The Age of Knowledge

In this zpp. When the user chooses a photo or takes a photo to create a photo. Simply click "Start to Identify" button to run the upload operation,

The code is as follows:

Uploadimgvar url,tsrc;function uploadimg () {    var src = $ (' #faceImg '). attr (' src ');    if (src) {        beginw ();                if (tsrc && tsrc = = src && url) {            Facepp ();        } else{            tsrc = src;            var token = Qiao.qiniu.uptoken (src);            var filename = qiao.qiniu.file;            Qiao.h.upload ({                URL: ' http://upload.qiniu.com/',                filepath:src,                datas: [                    {key: ' key ', value: filename},                    {key: ' token ', value:token}                ],                success:function () {                    URL = qiao.qiniu.url ();                    Facepp ();                },                fail:function (s) {                    showres (' Upload file failed: ' + s);}}            );        }    } else{        showres (' Please select the photo you want to recognize first!

');} }

To prevent uploading photos every time you click. So I made an inference. Suppose that the photo src in the app does not change and does not upload.

Otherwise, the encapsulated qiao.h.upload will be used for uploading.

If the upload fails, give the hint. Upload success will be image recognition.


Many other tutorials:

Hbuilder Development App Combat 1-age: HTTP://UIKOO9.COM/BOOK/DETAIL/5

Many other study notes: Http://uikoo9.com/book

Hbuilder Development App Combat-age 03-File Upload

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.