cordova-plugin-file-transfer实现文件上传、下载整理(一)
2017-02-27 13:44
267 查看
一、cordova FileTransfer简介
1.这个插件定义了全局对象FileTransfer、FileUploadOptions用于简单上传下载处理,需要在设备的deviceready之后使用。
2.这个插件onprogress可以监听长传和下载的进度,事件阐述ProgressEvent
3.不支持断点续传。abort()方法可以终止传输,但是终止之后没有继续的功能。
4.这个插件的上传是基于http的上传,当然也可以使用其他方式比如ajax上传、websocket上传等
5.目前测试在cordova中form表单方式的上传失败
官方文档:http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/index.html
二、安装命令
三、API整理
FileTransfer对象 文件对象提供了一种HTTP Post或put请求多文件上传和下载
事件:
1.onprogress: 文件传送过程中触发,传入
ProgressEvent.lengthComputable 是否可计算大小
ProgressEvent.loaded 已经下载大小
ProgressEvent.total
总文件大小
方法:
1.upload(fileURL,serverURL,successCB,errorCB,options,trustAllHosts) 上传文件到服务器
fileURL: 文件的URL地址,例如:FilesystemURL,dataURL,covURL
server: 服务器地址,地址转换需要使用
successCallback: A callback that is passed a
errorCallback: A callback that executes if an error occurs retrieving the
Invoked with a
options: Optional parameters (Object). Valid keys:
fileKey: The name of the form element. Defaults to
(DOMString)
fileName: 保存服务器上文件时使用的文件名。 Defaults to
httpMethod: The HTTP method to use - either
Defaults to
mimeType: 上传的mime类型. 默认值
params: 一组可选的键/值对HTTP请求中传递。(对象,键/值- DOMString)
chunkedMode:是否上传数据块流模式。默认为真。(布尔)
headers:头名称/头值的映射。使用数组指定多个值。在iOS和Android,FireOS,,如果头文件命名内容类型,多形式的数据将不会被使用。(对象)
trustAllHosts: 可选参数,默认为false。如果设置为true,则接受所有安全证书。这是有用的,因为Android拒绝自签名的安全证书。不推荐用于生产使用。支持Android和iOS。
2.download() 下载文件
source: 下载地址,需要加密可以使用
target: 文件系统URL表示设备上的文件。对于向后兼容性,这也可以是设备上文件的完整路径。
successCallback: A callback that is passed a
errorCallback: A callback that executes if an error occurs when retrieving the
Invoked with a
trustAllHosts: 可选参数,默认为false。如果设置为true,则接受所有安全证书。这是有用的,因为Android拒绝自签名的安全证书。不推荐用于生产使用。支持Android和iOS
options: 可选参数,目前只支持头文件(如授权).
3.abort() 终止传输进程
中止进行转移。onerror回调是通过filetransfererror对象具有filetransfererror.abort_err错误代码。
FileUploadResult对象
bytesSent: 发送到服务器的字节数,作为上载的一部分。. (long)
responseCode: 服务器返回的HTTP响应代码。 (long)
response:服务器返回的HTTP响应。(DOMString)
headers:由服务器的HTTP响应头. (Object),Currently supported on iOS only.
FileTransferError对象
在errorDB是,异常方法中传入参数对象
code: One of the predefined error codes listed below. (Number)
source: URL to the source. (String)
target: URL to the target. (String)
http_status: HTTP status code. This attribute is only available when a response code is received from the HTTP connection. (Number)
body Response body. This attribute is only available when a response is received from the HTTP connection. (String)
exception: Either e.getMessage or e.toString (String)
2 =
3 =
4 =
5 =
FileEntry对象
在successCB中传入的对象
各对象的构造函数:
更多:cordova-plugin-file-transfer实现文件上传、下载整理(二)
cordova-plugin-file 文件操作整理(三)
Apache Cordova开发环境搭建(二)VS Code
cordova-plugin-vibration 设备震动整理
1.这个插件定义了全局对象FileTransfer、FileUploadOptions用于简单上传下载处理,需要在设备的deviceready之后使用。
2.这个插件onprogress可以监听长传和下载的进度,事件阐述ProgressEvent
3.不支持断点续传。abort()方法可以终止传输,但是终止之后没有继续的功能。
4.这个插件的上传是基于http的上传,当然也可以使用其他方式比如ajax上传、websocket上传等
5.目前测试在cordova中form表单方式的上传失败
官方文档:http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/index.html
二、安装命令
cordova plugin add cordova-plugin-file-transfer
三、API整理
FileTransfer对象 文件对象提供了一种HTTP Post或put请求多文件上传和下载
事件:
1.onprogress: 文件传送过程中触发,传入
ProgressEvent(Function)
ProgressEvent.lengthComputable 是否可计算大小
ProgressEvent.loaded 已经下载大小
ProgressEvent.total
总文件大小
方法:
1.upload(fileURL,serverURL,successCB,errorCB,options,trustAllHosts) 上传文件到服务器
fileURL: 文件的URL地址,例如:FilesystemURL,dataURL,covURL
server: 服务器地址,地址转换需要使用
encodeURI().
successCallback: A callback that is passed a
FileUploadResultobject. (Function)
errorCallback: A callback that executes if an error occurs retrieving the
FileUploadResult.
Invoked with a
FileTransferErrorobject. (Function)
options: Optional parameters (Object). Valid keys:
fileKey: The name of the form element. Defaults to
file.
(DOMString)
fileName: 保存服务器上文件时使用的文件名。 Defaults to
image.jpg. (DOMString)
httpMethod: The HTTP method to use - either
PUTor
POST.
Defaults to
POST. (DOMString)
mimeType: 上传的mime类型. 默认值
image/jpeg. (DOMString)
params: 一组可选的键/值对HTTP请求中传递。(对象,键/值- DOMString)
chunkedMode:是否上传数据块流模式。默认为真。(布尔)
headers:头名称/头值的映射。使用数组指定多个值。在iOS和Android,FireOS,,如果头文件命名内容类型,多形式的数据将不会被使用。(对象)
trustAllHosts: 可选参数,默认为false。如果设置为true,则接受所有安全证书。这是有用的,因为Android拒绝自签名的安全证书。不推荐用于生产使用。支持Android和iOS。
2.download() 下载文件
source: 下载地址,需要加密可以使用
encodeURI().
target: 文件系统URL表示设备上的文件。对于向后兼容性,这也可以是设备上文件的完整路径。
successCallback: A callback that is passed a
FileEntryobject. (Function)
errorCallback: A callback that executes if an error occurs when retrieving the
FileEntry.
Invoked with a
FileTransferErrorobject. (Function)
trustAllHosts: 可选参数,默认为false。如果设置为true,则接受所有安全证书。这是有用的,因为Android拒绝自签名的安全证书。不推荐用于生产使用。支持Android和iOS
options: 可选参数,目前只支持头文件(如授权).
3.abort() 终止传输进程
中止进行转移。onerror回调是通过filetransfererror对象具有filetransfererror.abort_err错误代码。
FileUploadResult对象
bytesSent: 发送到服务器的字节数,作为上载的一部分。. (long)
responseCode: 服务器返回的HTTP响应代码。 (long)
response:服务器返回的HTTP响应。(DOMString)
headers:由服务器的HTTP响应头. (Object),Currently supported on iOS only.
FileTransferError对象
在errorDB是,异常方法中传入参数对象
code: One of the predefined error codes listed below. (Number)
source: URL to the source. (String)
target: URL to the target. (String)
http_status: HTTP status code. This attribute is only available when a response code is received from the HTTP connection. (Number)
body Response body. This attribute is only available when a response is received from the HTTP connection. (String)
exception: Either e.getMessage or e.toString (String)
Constants
1 =FileTransferError.FILE_NOT_FOUND_ERR
2 =
FileTransferError.INVALID_URL_ERR
3 =
FileTransferError.CONNECTION_ERR
4 =
FileTransferError.ABORT_ERR
5 =
FileTransferError.NOT_MODIFIED_ERR
FileEntry对象
在successCB中传入的对象
各对象的构造函数:
console.info(FileTransfer); console.info(FileUploadResult); console.info(FileError); console.info(FileUploadOptions);
更多:cordova-plugin-file-transfer实现文件上传、下载整理(二)
cordova-plugin-file 文件操作整理(三)
Apache Cordova开发环境搭建(二)VS Code
cordova-plugin-vibration 设备震动整理
相关文章推荐
- cordova-plugin-file-transfer实现文件上传、下载整理(二)
- Cordova - fileTransfer插件的使用详解(实现文件上传、下载)
- cordova-plugin-file-transfer 文件上传
- wordpress中inlineUpload plugin 与WP-Filebase plugin 集成实现文件的上传下载
- 关于$cordovaFileTransfer上传文件和参数遇到的坑
- cordova-plugin-file 文件操作整理(一)
- flex使用filereference+httphandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- cordova-plugin-file 文件操作整理(三)
- 利用flex+sevlet+fileupload组件实现文件的上传和下载
- 使用FileReference+HttpHandler实现文件上传/下载(Flex与.NET互操作)
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作 使用FileReference+HttpHandler实现文件上传/下载
- 文件下载cordovaFileTransfer:cordova.file.documentsDirectory is null
- flex使用filereference+httphandler实现文件上传/下载(转)
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- AjaxFileUpload实现文件异步上传(AjaxFileUpload.js文件下载及参数介绍)
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载