1、利用模板技术动态生成plist文件的内容:(模板内容和data替换为plist需要的内容)
//artTemplate
<script src="js/template.js" type="text/javascript" charset="utf-8"></script>
<script id="test" type="text/html">
<h1>{{title}}</h1>
<ul>
{{each list as value i}}
<li>索引 {{i + 1}} :{{value}}</li>
{{/each}}
</ul>
</script>
<script type="text/javascript">
var data = {
title: '标签',
list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']
};
var html = template('test', data);
</script>
2、将得到的内容生成文件对象:
var blob = new Blob([html], {type: "text/plain"});
3、利用ajax上传到七牛云服务:
var Qiniu_UploadUrl = "http://up.qiniu.com";
var Qiniu_upload = function(f, token) {
var xhr = new XMLHttpRequest();
xhr.open('POST', Qiniu_UploadUrl, true);
var formData, startDate;
formData = new FormData();
formData.append('token', token);
formData.append('name', 'xucheng.plist');
formData.append('chunk', 0);
formData.append('chunks', 1);
formData.append('key', 'xucheng.plist');
formData.append('file', f,'xucheng.plist');
var taking;
xhr.upload.addEventListener("loadstart", function(up,file){
console.log('开始上传')
}, false);
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var nowDate = new Date().getTime();
taking = nowDate - startDate;
var x = (evt.loaded) / 1024;
var y = taking / 1000;
var uploadSpeed = (x / y);
var formatSpeed;
if (uploadSpeed > 1024) {
formatSpeed = (uploadSpeed / 1024).toFixed(2) + "Mb/s";
} else {
formatSpeed = uploadSpeed.toFixed(2) + "Kb/s";
}
console.log(formatSpeed)
}
}, false);
xhr.onreadystatechange = function(response) {
if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText != "") {
var blkRet = JSON.parse(xhr.responseText);
console.log("上传成功")
} else if (xhr.status != 200 && xhr.responseText) {
}
};
xhr.send(formData);
};
//去后台请求token
$.ajax({
type:"get",
url:"/appstore/app/qiniuUploadToken?params=xucheng.plist",
async:true,
dataType:"json",
success:function(data){
var token =data.uptoken;
console.log(token);
Qiniu_upload(blob, token);
}
});
4、plist文件样例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>https://123.103.9.205:443/mobem/portal/download?filename=/Default/portal_ios.ipa</string>
</dict>
<dict>
<key>kind</key>
<string>full-size-image</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>https://123.103.9.205:443/mobem/portal/download?filename=/Default/portal_icon.png</string>
</dict>
<dict>
<key>kind</key>
<string>display-image</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>https://123.103.9.205:443/mobem/portal/download?filename=/Default/portal_icon.png</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>com.YYJZPortal</string>
<key>bundle-version</key>
<string>1.0</string>
<key>kind</key>
<string>software</string>
<key>subtitle</key>
<string></string>
<key>title</key>
<string>名称</string>
</dict>
</dict>
</array>
</dict>
</plist>