使用ionic开发webapp进行文件上传与下载
2016-04-11 22:09
549 查看
1.添加依赖的插件
1.org.apache.cordova.file-transfer
https://github.com/apache/cordova-plugin-file-transfer
This plugin allows you to upload and download files.
This plugin defines global FileTransfer, FileUploadOptions Constructors.
Although in the global scope, they are not available until after the deviceready event.
Installation
cordova plugin add cordova-plugin-file-transfer
2. org.apache.cordova.file
https://github.com/apache/cordova-plugin-file
This plugin implements a File API allowing read/write access to files residing on the device.
Installation
cordova plugin add cordova-plugin-file
2.保证你的项目中已经集成并且引用了ngCordova插件在你的
alert(JSON.stringify(err)); }, function (progress) { // constant progress updates $timeout(function () { $scope.downloadProgress = (progress.loaded / progress.total) * 100; }) }); }});
1.org.apache.cordova.file-transfer
https://github.com/apache/cordova-plugin-file-transfer
This plugin allows you to upload and download files.
This plugin defines global FileTransfer, FileUploadOptions Constructors.
Although in the global scope, they are not available until after the deviceready event.
Installation
cordova plugin add cordova-plugin-file-transfer
2. org.apache.cordova.file
https://github.com/apache/cordova-plugin-file
This plugin implements a File API allowing read/write access to files residing on the device.
Installation
cordova plugin add cordova-plugin-file
2.保证你的项目中已经集成并且引用了ngCordova插件在你的
app.controller('MyCtrl', function($scope, $timeout, $cordovaFileTransfer) { $scope.downloadFile = function() { var url = "http://your_ip_address/images/my.jpg"; var filename = url.split("/").pop(); alert(filename); var targetPath = cordova.file.externalRootDirectory + filename; var trustHosts = true var options = {}; //url提交的服务器地址 targetPath提交图片的本地地址 $cordovaFileTransfer.download(url, targetPath, options, trustHosts) .then(function(result) { // Success! alert(JSON.stringify(result));//把对象转化成字符串 }, function(error) { // Error alert(JSON.stringify(error)); }, function (progress) { $timeout(function () { $scope.downloadProgress = (progress.loaded / progress.total) * 100; }) }); } $scope.uploadFile = function() { var url = "http://your_ip_address/uploads/upload.php"; //target path may be local or url var targetPath = "http://your_ip_address/images/my.jpg"; var filename = targetPath.split("/").pop(); var options = { fileKey: "file", fileName: filename, chunkedMode: false, mimeType: "image/jpg" }; <pre name="code" class="javascript"> //url提交的服务器地址 targetPath提交图片的本地地址$cordovaFileTransfer.upload(url, targetPath, options).then(function(result) { console.log("SUCCESS: " + JSON.stringify(result.response)); alert("success"); alert(JSON.stringify(result.response)); }, function(err) { console.log("ERROR: " + JSON.stringify(err));
alert(JSON.stringify(err)); }, function (progress) { // constant progress updates $timeout(function () { $scope.downloadProgress = (progress.loaded / progress.total) * 100; }) }); }});
相关文章推荐
- webAPP前端必备知识
- WebApp中的页面生命周期及路由管理
- Eclipse+Maven创建webapp项目
- webAPP前端必备知识
- Spring3.1.1+Jersey2.2+ehcache实现WebApp与服务器接口交互获取令牌及校验过程
- 基于CSS3的WEBAPP横向滑动模式演化
- nodejs+ionic+cordova+intellijIdea搭建webApp环境
- (webapp)微信和safri 对于html5 部分功能不兼容,多选或单选下拉框去除边框无效果。
- java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files
- 【F2E】移动端(webapp)开发性能优化方案
- SpringMVC中的DispatcherServlet (及WebApplicationContext部分)
- spring中的Log4jConfigListener作用和webapp.root的设置
- 安卓app开发方式之webApp
- Jetty之WebAppContext
- maven自定义webapp目录
- webappbuilder框架中获取URL传参的方法
- android开发系列之一—创建一个简单的webapp
- webapp和mobileapp分析
- WebApp之 apple-touch-icon
- Azure Stack PaaS-网站(WebApp)背后是什么原理? 推荐