cordova upload pictures, video uploads (upload a single picture, a single video)

friendly reminder:

If you ensure that the project which will upload a picture can not following methods:

Only download Cordova-plugin-camera to get a picture or video to bring them into base64 format, and then converted to base64 File, the File and then you can upload them into formdata,

If you can not guarantee, use the following methods:

Other acquired multiple images plugin will default cordova-plugin-file is installed, it will change among the js file format, upload it into formdata be wrong, see how to modify upload multiple images

 

1. Download plugin

cordova plugin add cordova-plugin-file-transfer // upload pictures plug-in (you can only upload one)

cordova plugin add cordova-plugin-camera // get pictures, video plug, one can only get a

cordova plugin add cordova-plugin-file // will change the file format js

2. Obtain pictures, videos

navigator.camera.getPicture ((the imageData) => { 
      the console.log (the imageData) 
function // Photo video
uploadImage (the imageData) }, (ERR)
=> { the console.log (ERR) }, { // Configure destinationType : Camera.DestinationType.FILE_URI, the sourceType: Camera.PictureSourceType.PHOTOLIBARY, AllowEdit: to true })

配置API:https://www.w3cschool.cn/cordova/cordova_camera.html

3. Upload

function uploadImage (fileURL) {
           var . ft = new new the FileTransfer ()
           var Options = new new FileUploadOptions () 
      // corresponding to the background field options.fileKey
= 'Avatar' options.fileName = fileURL.substr (fileURL.lastIndexOf ( '/') + . 1 ); var URI = 'upload address' ft.upload ( fileURL, the encodeURI (URI), (MSG) => { the console.log ( 'SUCCESS:' ) the console.log (MSG) }, (ERR)=>{ console.log('ERROR:') console.log(err) }, options ) },

配置api:https://www.javascriptcn.com/read-60998.html

 

Guess you like

Origin www.cnblogs.com/guan-shan/p/11579837.html