Micro-Letter Applet Wx.uploadfile cannot upload solution _javascript

Source: Internet
Author: User

Micro-Letter Applet Wx.uploadfile can't upload solution

Micro-Android clients are not able to use the Wx.uploadfile upload file problems Many developers have encountered.

I also because has not been able to solve, the bite of the bullet to submit audit finally rejected (with the auditor is best to use iOS detection mentality, unfortunately, I applied the use of Android), before trying to use the means of third-party solutions.

In the end, I used seven cattle third party storage, the file directly uploaded to seven cattle storage space and then callback use.

Of course, like a cloud, the Vientiane excellent map these third-party storage sources can be used in this way.

First of all, the seven-cow https upload domain name into the small program domain name list.

Here I am using the domain name https://up.qbox.me of the seven cattle eastern area.

Micro-Letter Applet wx.uploadfile use is the multipart/form-data way to upload, that is, form upload.

According to the official document of seven cattle http://developer.qiniu.com/docs/... up/form-upload.html

The main need is file (the document itself) and token (upload credentials).

Then deploy the business code for the upload voucher on the server side and get the token through the wx.request request.

   var that = this;

   Wx.request ({

    URL: ' Https://xxx/token ', method

    : ' POST ',

    data: {},

    header: {

     ' content-type ': ' Application/x-www-form-urlencoded '

    },

    success:function (res) {

      That.token = res.data;//default returns a token, Assignment to an already existing token property. Here is the example, the specific requirements can be set up on their own.

    },

    fail:function (res) {

     Console.log (res)

    }

   })

Specific how to deploy seven cattle voucher code, can refer to Http://78re52.com1.z0.glb.cloudd ...%9c%8d%e5%8a%a1.pdf

When you get the voucher, you can upload it via wx.uploadfile. The specific code is:

 var that = this;

  var key = Math.random (). toString. substr (2); Generates a random string of filename

  wx.uploadfile ({

   URL: ' https://up.qbox.me ',

   Filepath:flie,

   name: ' File ',

   formdata:{

    ' token ': that.token,//just acquired the upload voucher

    ' key ': key//here is to set the file name after uploading for files

   },

   success:function (r) {

    var data = r.data;//Seven cow returns a JSON string containing the hash value and key (

    typeof data=== ' string ') data = Json.parse (Data.trim ()); /decompression

    if (data.key) {

     ...///Here you can use Data.key directly, the file has been uploaded successfully can be used. If it is a picture can also be called directly through the image.

    }

   ,

   fail:function (res) {

    Console.log (res)

   }

  })

Finally this way to solve the problem of Android can not upload files, of course, upload their own server problem remains to be solved.

However, developers who are in urgent need of solutions offer a workable solution.

Now using a Third-party source or storage source is a common solution, many storage sources such as seven cows, and cloud, the Vientiane Excellent map provides free space for developers to use.

We also suggest that we should do more to construct and try this area.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.