您的位置:首页 > Web前端

【翻译】【Ionic】上传,FileTransfer学习

2017-07-19 19:08 197 查看
英文原文及GitHub下载地址点我

FileTransfer

对象提供一种使用HTTP多部分 POST或者PUT请求上传文件的方式,并且,可以下载文件;

Properties

onprogress:无论是否一个新的大量数据被传输了,其总会伴随ProgressEvent事件;

Method

upload:向服务器上传一个文件;

download:从服务器下载一个文件;

abort:终止一个运行的传输程序;

upload

参数:

fileURL:文件系统的URL代表设备上一个文件或数据URI。为了能够兼容性反馈,也可以是文件在设备上的全路径;

server:服务器接收文件的URL,可被encodeURI编码;

successCallBack:返回一个FIleUploadResult对象,文件上传结果对象,用来反馈是否通过;

errorCallBack:错误反馈,如果一个错误在检索过程中发生,其会引用一个FileTransferError文件传输错误对象;

options:操作性参数,可得到的keys:

fileKey:文件名,默认为file;

fileName:存储文件到服务器的名字,默认为image.jpg;

httpMethod:http请求方法,有POST或PUT,默认为POST;

mimeType:上传的数据的类型,默认是image/jpeg;

mime:用途互联网邮件扩展类型。是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。

params:http请求中,一个操作性键值对的集合,(Object,key/value)

chunkedMode:是否上传数据使用大量数据模式;默认true;(boolean)

headers:一个headerMap类型 name/header value键值对;使用hash来唯一确定某个值;在IOS,FireOS和安卓Android中,如果一个header中的Content-Type的Map存在,多部分形式数据将不会被使用;

trustAllHosts:操作性参数,默认是false;如果设置为true的时候,其将接受安全验证,自从安卓拒绝self-signed安全验证之后,这是很有用;不推荐产品使用。支持安卓和IOS;(boolean类型)

例子:

// !! Assumes variable fileURL contains a valid URL to a text file on the device,
//    for example, cdvfile://localhost/persistent/path/to/file.txt

var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}

var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "text/plain";

var params = {};
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(fileURL, encodeURI("http://some.server.com/upload.php"), win, fail, options);


上传头部和项目时间的例子(只支持Android和IOS)

function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}

function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}

var uri = encodeURI("http://some.server.com/upload.php");

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=fileURL.substr(fileURL.lastIndexOf('/')+1);
options.mimeType="text/plain";

var headers={'headerParam':'headerValue', 'headerParam2':'headerValue2'};

options.headers = headers;

var ft = new FileTransfer();
ft.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
} else {
loadingStatus.increment();
}
};
ft.upload(fileURL, uri, win, fail, options);


FileUploadResult

一个FileUploadResult 对象是文件传输(FileTransfer)和上传(upload)通过成功的反馈;

Properties

bytesSent:发送到服务器的字节数,一同上传;(long)

responseCode:服务器返回的HTTP反馈代码;(long)

response:服务器返回的HTTP响应;(DOMString)

headers:服务器的HTTP响应头部;(Object)目前只支持IOS;

**IOS Quirks**IOS兼容

不支持responseCode或bytesSent;

不支持上传带有chunkedMode=true和multipartMode=false的空文件;

Browser Quirks浏览器兼容

withCredentials:boolean告诉浏览器在XMLHttpRequest上设置withCredentials标记;

**Windows Quirks**windows兼容

一个对参数的操作,因为windows API的设计,把空/null value排除在上传操作之外;

chunkedMode 是一个不支持并且设置非chunked模式;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: