From a0a654b8b505194931ca8f0b3c88d6d38666fcbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=9C=E4=BF=8A=E6=A0=87?= Date: Fri, 9 Oct 2015 00:02:26 +0800 Subject: [PATCH] codova file upload --- lib/upload.js | 89 ++++++++++++++++++++++++++++++++++++++++++++------- package.js | 2 +- 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/lib/upload.js b/lib/upload.js index 6cbf7e1..9ea0d62 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -11,8 +11,9 @@ Slingshot.Upload = function (directive, metaData) { - if (!window.File || !window.FormData) { - throw new Error("Browser does not support HTML5 uploads"); + if (!window.File || !window.FormData ) { + if(!cordova || !cordova.file || !FileTransfer) + throw new Error("Browser does not support HTML5 uploads and cordova file transfer is not available"); } var self = this, @@ -34,6 +35,54 @@ Slingshot.Upload = function (directive, metaData) { return formData; } + function cordova_transfer(callback){ + status.set("transferring"); + loaded.set(0); + + var ft = new FileTransfer(); + + ft.onprogress = function(progressEvent){ + if(progressEvent.lengthComputable){ + loaded.set(progressEvent.loaded); + total.set(progressEvent.total); + } + }; + + var options = new FileUploadOptions(); + options.headers = {}; + _.each(self.instructions.headers, function (value, key) { + options.headers[key] = value; + }); + options.headers['Content-Length'] = self.file.size; + + + options.params = {}; + _.each(self.instructions.postData, function (value) { + options.params[value.name] = value.value; + }); + + var fileUrl = self.file.nativeURL; + options.fileKey = "file"; + options.fileName = self.file.name; + options.mimeType = self.file.type; + //options.chunkMode = false; + options.httpMethod = 'post'; + + + ft.upload(fileUrl,encodeURI(self.instructions.upload),function(result){ + status.set("done"); + loaded.set(total.get()); + callback(null, self.instructions.download); + + },function(err){ + status.set("failed"); + callback(new Meteor.Error(err.http_status,"Failed to upload file to cloud storage",err.exception)); + },options); + + self.xhr = ft; + + return self; + } _.extend(self, { /** @@ -86,20 +135,34 @@ Slingshot.Upload = function (directive, metaData) { */ send: function (file, callback) { - if (! (file instanceof window.File) && ! (file instanceof window.Blob)) + if (! (file instanceof window.File) && ! (file instanceof window.Blob) && !( typeof file==='string' && /^file:\/\/.*$/i.test(file)) ) throw new Error("Not a file"); - self.file = file; + function _send(){ + self.request(function (error, instructions) { + if (error) { + return callback(error); + } - self.request(function (error, instructions) { - if (error) { - return callback(error); - } + self.instructions = instructions; - self.instructions = instructions; + self.transfer(callback); + }); + } + + if(/^file:\/\/.*$/i.test(file)) { + window.resolveLocalFileSystemURL(file, function (fileEntry) { + fileEntry.file(function (f) { + self.file = f; + self.file.nativeURL = fileEntry.nativeURL; + _send(); + }) + }) + }else{ + self.file = file; + _send(); + } - self.transfer(callback); - }); return self; }, @@ -146,6 +209,10 @@ Slingshot.Upload = function (directive, metaData) { throw new Error("Cannot transfer file at upload status: " + status.curValue); } + if(self.file.hasOwnProperty('nativeURL'))//cordova File + { + return cordova_transfer(callback); + } status.set("transferring"); loaded.set(0); diff --git a/package.js b/package.js index 1fdec4e..b9ee502 100644 --- a/package.js +++ b/package.js @@ -1,7 +1,7 @@ Package.describe({ name: "edgee:slingshot", summary: "Directly post files to cloud storage services, such as AWS-S3.", - version: "0.7.1", + version: "0.7.1-test", git: "https://github.com/CulturalMe/meteor-slingshot" });