目录

$file

$file是提供上传下载文件的服务对象

$file.upload()

上传文件,分以下两种情况

1、上传到Tomcat服务器
上传的文件保存在tomcat的apache-tomcat-7.0.57webappsumserverdownload目录下。如果使用我们提供的tomcat只需将tomcat拷贝即可,不需要配置任何信息,如果是自己的tomcat等,只需将我们提供的umserver文件夹拷贝到tomcat对应的web目录下,tomcat即webapps下。
启动tomcat时进入目录apache-tomcat-7.0.57in,打开startup.bat批处理文件,tomcat正常启动的前提是必须配有JAVA_HOME。使用上传下载服务后上传的文件保存在apache-tomcat-7.0.57webappsumserverdownload目录下。

2、上传到MA2.7服务器
上传到MA2.7服务器上,需要对MA进行文件服务器的配置(MA2.7自带一个文件服务器),如下图:

进行如图配置之后,可直接通过$file.upload将文件上传到MA的服务器上
注意:上传之后会返回一个key,请将key妥善保存,因为如果要想去MA取上传的这个文件key是门票

语法

  1. $file.upload({
  2. "url" : "http://10.2.112.22:8080/umserver/upload", //上传服务器端路径
  3. "filename" : "xxx/xxx/file.xxx", //上传文件的路径+文件名
  4. "bindfield" : "fileInfo", //上传后的返回值,类型为JSONObject(其中从键值url可以获取上传后该文件的url)
  5. "callback" : "uploadCB()"//上传后的回调方法
  6. })

参数

  • url:上传服务器端路径,即配有tomcat的电脑的IP,每次只要替换IP、Port即可
  • filename:上传文件的路径+文件名
  • bindfield:上传后的返回值,类型为JSONObject(其中从键值url可以获取上传后该文件的url)
  • callback:上传后的回调方法
  • compressionRatio:按比例压缩图片大小,值为0-1,例如:”compressionRatio” : 0.5

实例

  1. var flag = -1;
  2. var filename = "image.jpg";
  3. $file.upload({
  4. "url" : "http://10.2.112.22:8080/umserver/upload",
  5. "filename" : "xxx/xxx/file.xxx",
  6. "bindfield" : "upload",
  7. "callback" : function uploadImageCallback() {
  8. var upload = $ctx.getString("upload");
  9. upload = $stringToJSON(upload); //upload.url即上传文件存放的URL,可根据该URL下载该文件
  10. $alert("服务器上文件存放的URL为:" + upload.url);
  11. flag = flag + 1;
  12. var badge = (upload.url).split(".");
  13. filename = "image" + flag + "." + badge[badge.length - 1];
  14. $alert("filename:" + filename);
  15. $file.download({
  16. "url" : upload.url,
  17. "locate" : "downloadTest/image",
  18. "filename" : filename,
  19. "override" : "true",
  20. "callback" : "downloadfromserverCB()"
  21. });
  22. }
  23. });
  24. function downloadfromserverCB() {
  25. var value = "downloadTest/image/" + filename;
  26. $alert("value = " + value);
  27. $id("image0").set("src", value);
  28. }

$file.download()

下载文件

语法

  1. $file.download({
  2. "url" : "http://xxx/xxx/xxx.png", //下载文件的url
  3. "locate" : "download/image", //下载后文件存放的路径
  4. "filename" : "newfile.png", //下载后重命名的文件名
  5. "override" : "true", //下载后是否覆盖同名文件
  6. "callback" : "downloadCB()"//下载后的回调方法,locate+filename可以访问文件(即download/image/newfile.png)
  7. })

参数

  • url:下载文件的url
  • filename:下载后重命名的文件名
  • locate:下载后文件存放的路径
  • override:下载后是否覆盖同名文件
  • callback:下载后的回调方法,locate+filename可以访问文件

实例

  1. $file.download({
  2. "url" : "http://misc.360buyimg.com/lib/img/e/logo-201305.png",
  3. "locate" : "downloadTest/image",
  4. "filename" : "jd.png",
  5. "override" : "true",
  6. "callback" : "downloadImageCallback()"
  7. });

$file.open()

打开文件服务

语法

  1. $file.open({
  2. "filename" : "log.txt", //文件全路径
  3. "filetype" : "txt", //支持手机能打开的格式*.txt,*.doc,*.pdf等
  4. "filepath" : "myfiles/mytxt/"
  5. })

参数

  • filename:文件名
  • filetype:支持手机能打开的格式.txt,.doc,*.pdf等
  • filepath:文件路径

$file.getFileInfo()

获取文件信息的服务

语法

  1. $file.getFileInfo(filePath)//文件全路径,如"xxx/xxx/file.txt"
  2. $file.getFileInfo({"path" : "xxx/xxx/file.xxx"})

参数

  • filePath:文件所在路径
  • path:绑定字段

实例

  1. var info = $file.getFileInfo(“xxx/xxx.doc”);
  2. $file.getFileInfo({
  3. "path" : "xxx/xxx/file.xxx"
  4. })
  5. var info = $ctx.getString("path")

$file.ftpUpload()

Ftp文件上传

语法

  1. $file.ftpUpload({
  2. "host" : "10.2.112.44",
  3. "port" : "21",
  4. "username" : "UAPFTP",
  5. "password" : "UAPFTP",
  6. "localFileFullName" : "/storage/emulated/0/DCIM/xxx.jpg",
  7. "compressionRatio" : "0.5",
  8. "remotePath" : "/UAPAndroid/test/sunny/",
  9. "remoteFileName" : "newname.jpg",
  10. "callback" : "my callback()"
  11. })

参数

  • host:服务器ip
  • port:服务器端口
  • username:登陆ftp的用户名
  • password:登陆ftp的密码
  • localFileFullName:手机端相对路径下的文件名
  • compressionRatio:按比例压缩,值为0-1
  • remotePath:上传到服务端的路径
  • remoteFileName:上传到服务器端文件名,默认取原文件名
  • callback:可选,上传成功后回调

实例
【对应的CSS】

  1. <div id="panel0">
  2. <input id="textbox0" maxlength="256" placeholder="host" value="10.2.112.27" type="text"/>
  3. <input id="textbox1" maxlength="256" placeholder="port" value="21" type="text"/>
  4. </div>
  5. <div id="panel2">
  6. <input id="textbox4" maxlength="256" placeholder="username" value="UAPFTP" type="text"/>
  7. <input id="textbox5" maxlength="256" placeholder="password" value="UAPFTP" type="text"/>
  8. </div>
  9. <input id="textbox6" maxlength="256" placeholder="remotePath" value="/UAPAndroid/test/sunny/" type="text"/>
  10. <input id="button0" value="点击上传" class="textbtnclass" onclick="this.button0_onclick()" type="button"/>
  11. <imageselector id="imageselector0"/>
  12. <input id="button1" value="点击下载" class="textbtnclass" onclick="this.button1_onclick()" type="button"/>
  13. <image id="image0" scaletype="fitcenter" src="picture"/>

【对应JS】

  1. //上传
  2. var host;
  3. var port;
  4. var username;
  5. var password;
  6. var remotePath;
  7. function com$requirement0724$Ftp1Controller$button0_onclick(sender, args) {
  8. host = $id("textbox0").get("value");
  9. port = $id("textbox1").get("value");
  10. username = $id("textbox4").get("value");
  11. password = $id("textbox5").get("value");
  12. remotePath = $id("textbox6").get("value");
  13. var str1 = $id("imageselector0").get("selectedimages");
  14. var str = $stringToJSON(str1);
  15. for (var i = 0; i < str.length; i++) {
  16. var pic = str[i]
  17. var index = pic.lastIndexOf("/");
  18. var path = pic.substring(0, index);
  19. $file.ftpUpload({
  20. "host" : "10.2.112.44",
  21. "port" : "21",
  22. "username" : "UAPFTP",
  23. "password" : "UAPFTP",
  24. "localFileFullName" : "/storage/emulated/0/DCIM/xxx.jpg", //相对路径下的文件名
  25. "compressionRatio" : "0.5", //取值范围0-1
  26. "remotePath" : "/UAPAndroid/test/sunny/", //上传路径
  27. "remoteFileName" : "newname.jpg", //上传后的新的文件名,如未指定,则取原文件名
  28. "callback" : "uploadcallback()"
  29. });
  30. }
  31. }
  32. function uploadcallback(sender, args) {
  33. $alert("上传完成:args:" + $jsonToString(args));
  34. }

$file.ftpDownload()

Ftp文件下载

语法

  1. $file.ftpDownload({
  2. "host" : "10.2.112.44",
  3. "port" : "21",
  4. "username" : "UAPFTP",
  5. "password" : "UAPFTP",
  6. "remoteFileFullName" : "/UAPAndroid/test/sunny/serverFile.jpg",
  7. "localPath" : "/storage/emulated/0/aaaaanewPath/ccc/",
  8. "localFileName" : "newname.jpg",
  9. "callback" : "my callback()"
  10. })

参数

  • host:服务器ip
  • port:服务器端口
  • username:登陆ftp的用户名
  • password:登陆ftp的密码
  • localPath:下载路径
  • localFileName: 下载后的新的文件名,如未指定,则取原文件名
  • callback:可选,上传成功后回调

实例
【对应的CSS】

  1. <div id="panel0">
  2. <input id="textbox0" maxlength="256" placeholder="host" value="10.2.112.27" type="text"/>
  3. <input id="textbox1" maxlength="256" placeholder="port" value="21" type="text"/>
  4. </div>
  5. <div id="panel2">
  6. <input id="textbox4" maxlength="256" placeholder="username" value="UAPFTP" type="text"/>
  7. <input id="textbox5" maxlength="256" placeholder="password" value="UAPFTP" type="text"/>
  8. </div>
  9. <input id="textbox6" maxlength="256" placeholder="remotePath" value="/UAPAndroid/test/sunny/" type="text"/>
  10. <input id="button0" value="点击上传" class="textbtnclass" onclick="this.button0_onclick()" type="button"/>
  11. <imageselector id="imageselector0"/>
  12. <input id="button1" value="点击下载" class="textbtnclass" onclick="this.button1_onclick()" type="button"/>
  13. <image id="image0" scaletype="fitcenter" src="picture"/>

【对应JS】

  1. //下载
  2. var i = 0;
  3. function com$requirement0724$Ftp1Controller$button1_onclick(sender, args) {
  4. var path = $service.call("UMDevice.getAppAlbumPath", {}, true)
  5. $file.ftpDownload({
  6. "host" : "10.2.112.44",
  7. "port" : "21",
  8. "username" : "UAPFTP",
  9. "password" : "UAPFTP",
  10. "remoteFileFullName" : "/UAPAndroid/test/sunny/serverFile.jpg",
  11. "localPath" : "/storage/emulated/0/aaaaanewPath/ccc/",
  12. "localFileName" : "newname.jpg", //下载后的新的文件名,如未指定,则取原文件名
  13. "callback" : "downloadcallback()"
  14. })
  15. }
  16. function downloadcallback(sender, args) {
  17. args = $stringToJSON(args);
  18. $alert("下载完成:" + args.downloadfilefullpath);
  19. $id("image0").set("src", args.downloadfilefullpath);
  20. }

$file.write()

此方法不开放,请使用等效方法$cache.write()的语法2

$file.read()

此方法不开放,请使用等效方法$cache.read()的语法2

$file.remove()

移除文件

语法:

  1. $file.removeFile(json);

参数:

  • json
    • path:基本文件系统,路径
    • filename:文件名
    • callback:删除完成之后的回调函数

示例:

  1. $file.removeFile({
  2. "path" : filepath,
  3. "file" : filename,
  4. "callback" : "removeCallback()"
  5. })
  6. function removeCallback(args) {
  7. alert(args.status);
  8. }

$file.exists()

判断文件是否存在。

语法:

  1. $file.exists(json);

参数:

  • json
    • path:基本文件系统,路径
    • file:文件名

返回值:

  • 类型:布尔

示例代码:

  1. $file.exists({
  2. path : filepath,
  3. file : filename
  4. });

$file.saveImageToAlbum()

保存图片到相册

语法

  1. $file.saveImageToAlbum({
  2. "override" : true,
  3. "path" : "", //文件路径
  4. "callback" : "saveCallback()"
  5. })

参数:

  • override:是否覆盖
  • file:文件路径
  • callback:回调

示例代码:

  1. $file.saveImageToAlbum({
  2. "override" : true,
  3. "path" : "image/IUAP_45672.jpg",
  4. "callback" : "saveCallback()"
  5. })

$file.openFileSelector()(仅安卓)(待添加)

打开文件选择器。

语法

  1. $file.openFileSelector({
  2. "path" : "", //文件路径
  3. "callback" : "saveCallback()"
  4. })

参数:

  • path:文件路径
  • callback:回调

示例代码:

  1. $file.openFileSelector({
  2. "path" : "image/IUAP_45672.jpg",
  3. "callback" : "saveCallback()"
  4. })

$file.fileToBase64()

文件转换成Base64串
注意:此方法限用文件大小在100kb以内的文件

语法

  1. var base64 = $file.fileToBase64({
  2. "filePath" : "" //文件路径
  3. })

参数:

  • filePath:文件路径

返回值:

  • base64:base64串

示例代码:

  1. var base64 = $file.fileToBase64({
  2. "filePath" : "image/IUAP_45672.jpg"
  3. })

$file.base64ToFile()

Base64串转换成文件
注意:此方法限用文件大小在100kb以内的文件

语法

  1. var ret = $file.base64ToFile({
  2. "base64" : "", //base64串
  3. "filePath" : "" //文件路径
  4. })

参数:

  • base64:base64串
  • filePath:文件路径

返回值:

  • ret:回返true或false

示例代码:

  1. var ret = $file.base64ToFile({
  2. "base64" : "",
  3. "filePath" : "image/IUAP_45672.jpg"
  4. })
  5. alert(ret);
文档更新时间: 2018-01-15 14:39