Skip to content

informat.storage 文件存储操作

概述

使用informat.storage对象进行文件存储操作,文件存储使用S3协议存储在文件服务器中。下方所有的文件都会存储在共享存储中。

术语说明

exists

判断文件是否存在

javascript
informat.storage.exists(path)
参数类型描述
pathString文件路径

返回值 Boolean

返回文件是否存在

示例

javascript
// 判断当前应用指定模块附件字段的文件是否存在
const path = 'moduleId/fieldId/fileId';
var result = informat.storage.exists(path);
if (result) {
  console.log('文件存在');
} else {
  console.log('文件不存在');
}

listFile

返回文件夹下的文件列表

javascript
informat.storage.listFile(path)
参数类型描述
pathString文件路径

返回值 Array<String>

返回文件夹下的文件列表

示例

javascript
//获取当前应用的所有文件
const fileList = informat.storage.listFile('/');
console.log('fileList',fileList);

返回数据如下:

javascript
[
    'fileId',
    'moduleId/fieldId/fileId',
    'moduleId/fieldId/fileId2',
    ...
]

move

将指定路径下的文件移动到目标路径下。注意:如果目标路径存在同名文件,将覆盖同名文件

javascript
informat.storage.move(source,target)
参数类型描述
sourceString源文件路径
targetString目标路径

返回值

示例

javascript
// 将指定文件移动到应用根目录
const source = 'moduleId/fieldId/fileId';
const target = 'fileId';
informat.storage.move(source, target);

copy

将指定路径下的文件拷贝到目标路径下。注意:如果目标路径存在同名文件,将覆盖同名文件

javascript
informat.storage.copy(source,target)
参数类型描述
sourceString源文件路径
targetString目标路径

返回值

示例

javascript
// 将指定文件拷贝一个副本文件
const source = 'moduleId/fieldId/fileId';
const target = 'moduleId/fieldId/fileIdCopy';
informat.storage.copy(source, target);

delete

删除文件

如果path对应的文件不存在,系统会报文件不存在错误; 如果path对应的是一个文件夹,不是文件,系统会报文件不存在错误;

javascript
informat.storage.delete(path)
参数类型描述
pathString文件路径

返回值

示例

javascript
// 删除当前应用指定模块附件字段的文件
const path = 'moduleId/fieldId/fileId';
informat.storage.delete(path);

deleteDirectory

删除文件夹

如果path对应的文件夹不存在,系统会报文件不存在错误; 如果path对应的是一个文件,不是文件夹,系统会报文件不存在错误;

javascript
informat.storage.deleteDirectory(path)
参数类型描述
pathString文件夹路径

返回值

示例

javascript
// 删除当前应用指定模块附件字段的所有文件
const path = 'moduleId/fieldId';
informat.storage.deleteDirectory(path);

download

将共享存储中的文件,下载到本地沙盒环境。注意:如果本地存在同名文件,将覆盖同名文件

javascript
informat.storage.download(path,localPath)
参数类型描述
pathString文件路径
localPathString本地沙盒环境文件路径

返回值

示例

javascript
// 下载当前应用,指定共享存储文件到本地根目录
const path = 'moduleId/fieldId/fileId';
const localPath = 'local.png';
informat.storage.download(path, localPath);

upload

将本地文件上传到共享存储中。注意:如果本地存在同名文件,将覆盖同名文件

javascript
informat.storage.upload(localPath,remotePath)
参数类型描述
localPathString本地文件路径
remotePathString共享存储路径

返回值

示例

javascript
// 上传当前应用,指定本地文件到共享存储根目录
const localPath = 'local.png';
const remotePath = 'remote.png';
informat.storage.upload(localPath, remotePath);

uploadFromURL

将远程地址指定的文件上传到共享存储中

javascript
informat.storage.uploadFromURL(url,remotePath)
参数类型描述
urlString远程地址
remotePathString共享存储的路径

返回值

示例

javascript
// 上传远程文件到当前应用共享存储根目录
const url = 'https:/example.com/file.jpg';
const remotePath = 'file.jpg';
informat.storage.uploadFromURL(url, remotePath);

getFilePath

获取文件路径

javascript
informat.storage.getFilePath(tableKey,fieldKey,fileId)
参数类型描述
tableKeyString数据表标识符
fieldKeyString字段标识符
fileIdString文件ID

返回值 String

共享存储文件路径

示例

javascript
// 获取数据表标识符为goods,字段标识符为pic,文件ID为fileId的文件路径
const filePath = informat.storage.getFilePath('goods', 'pic', 'fileId');
console.log('文件路径---->', filePath); // 文件路径---->goodsmoduleId/picFieldId/fileId

createFileToken

创建文件访问Token

javascript
informat.storage.createFileToken(path, fileName, expireTime)
参数类型描述
pathString文件共享存储的路径
fileNameString文件名称,可以为空
expireTimeInteger过期时间 单位:秒

createFileDownloadUrl

创建文件下载地址

javascript
informat.storage.createFileDownloadUrl(fileToken)
参数类型描述
fileTokenString文件访问TOKEN

createFileDownloadUrl

创建文件下载地址

javascript
informat.storage.createFileDownloadUrl(fileToken, host)
参数类型描述
fileTokenString文件访问TOKEN
hostStringweb服务地址 可以为空 默认为外网访问地址

convertFormat

文档格式转换

javascript
informat.storage.convertFormat(sourcePath,targetPath,setting)
参数类型描述
sourcePathString要转换的文档路径
targetPathString转换成功后目标文档路径
settingConvertSetting转换设置

convertFormat

文档格式转换

javascript
informat.storage.convertFormat(sourcePath,targetPath,onlyofficeServiceUrl,setting)
参数类型描述
sourcePathString要转换的文档路径
targetPathString转换成功后目标文档路径
onlyofficeServiceUrlStringonlyoffice服务地址,默认为管理后台配置的文件预览地址
settingConvertSetting转换设置

ConvertSetting 结构如下

js
{
    async:Boolean,//定义转换请求类型:异步与否
    fileType:String,//定义要转换的文档文件的类型。	
    outputtype:String,//定义生成的转换文档类型。	
    password:String,//如果文档文件受密码保护,则定义该文件的密码
    region:String,//定义从电子表格格式转换为pdf时货币和日期和时间的默认显示格式。使用四个字母(en-US、fr-FR等)语言代码进行设置。默认值为en-US。
    title:String,//定义转换后的文件名
    spreadsheetLayout:{
        fitToHeight:Integer,//设置转换区域的高度,以页数为单位。默认值为0。
        fitToWidth:Integer,//设置转换区域的宽度,以页数为单位。默认值为0。
        gridLines:Boolean,//允许在输出 PDF 文件中包含或不包含网格线。默认值为false。
        headings:Boolean,//允许在输出 PDF 文件中包含或不包含标题。默认值为false。
        ignorePrintArea:Boolean,//确定是否忽略为电子表格文件选择的打印区域。默认值为true
        margins:{
            bottom:String,//设置输出 PDF 文件的下边距。默认值为19.1 毫米。
            right:String,//设置输出 PDF 文件的右边距。默认值为19.1 毫米。
            left:String,//设置输出 PDF 文件的左边距。默认值为19.1 毫米。
            top:String,//设置输出 PDF 文件的上边距。默认值为19.1 毫米
        },
        orientation:String,//设置输出 PDF 文件的方向。可能是landscape,portrait。默认值为portrait。
        pageSize:{
            height:String,//设置输出 PDF 文件的页面高度。默认值为297 毫米。
            width:String//设置输出 PDF 文件的页面宽度。默认值为210 毫米。
        },
        scale:Integer,//允许设置输出 PDF 文件的比例。默认值为100。
    },
    thumbnail:{
        /*
        定义使图像适合指定的高度和宽度的模式。支持的值:0 - 拉伸文件以适应高度和宽度1 - 保持图像的方面2 - 在这种情况下,不使用宽度和高度设置。取而代之的是,页面的公制尺寸转换为 96dpi 的像素。例如,A4 (210x297mm) 页面将变成尺寸为 794x1123pix 的图片。默认值为:2。
        */
        aspect:Integer,
        /*
        定义是仅为第一页还是为所有文档页面生成缩略图。
        如果为 false,则将创建包含所有页面缩略图的 zip 存档。
        默认值为true,
        */
        first:Boolean,
        height:Integer,//以像素为单位定义缩略图高度。默认值为100
        width:Integer,//以像素为单位定义缩略图宽度。默认值为100
    }
}

支持的文件转换表

符号说明

  • ⬤ 表示完全支持该格式转换
  • 〇 表示不支持该格式转换

文本文档文件格式

输入格式bmpdocmdocxdotmdotxepubfb2gifhtmljpgodtottpdfpdfapngrtftxt
djvu
doc
docm
docx
dot
dotm
dotx
epub
fb2
fodt
htm
html
hwp
hwpx
mht
mhtml
odt
ott
oxps
pages
pdf
rtf
stw
sxw
txt
wps
wpt
xml
xps

电子表格文件格式

输入格式bmpcsvgifjpgodsotspdfpdfapngxlsmxlsxxltmxltx
csv
et
ett
fods
numbers
ods
ots
sxc
xls
xlsb
xlsm
xlsx
xlt
xltm
xltx
xml

演示文稿文件格式

输入格式bmpgifjpgodpotppdfpdfapngpotmpotxppsmppsxpptmpptx
dps
dpt
fodp
key
odp
otp
pot
potm
potx
pps
ppsm
ppsx
ppt
pptm
pptx
sxi

示例:

将文件从 docx 格式转换为 pdf 格式

js
informat.storage.convertFormat('table/field/fileId','table/field/fileId_converted',{
    "filetype": "docx",
    "outputtype": "pdf",
    "title": "test.pdf",
})

将文件从 docx 格式转换为 png 缩略图

js
informat.storage.convertFormat('table/field/fileId','table/field/fileId_converted',{
    "filetype": "docx",
    "outputtype": "png",
    "title": "test.png",
    "thumbnail": {
        "aspect": 0,
        "first": true,
        "height": 150,
        "width": 100
    }
})

将文件从 xlsx 格式转换为 pdf 格式

js
informat.storage.convertFormat('table/field/fileId','table/field/fileId_converted',{
    "filetype": "xlsx",
    "outputtype": "pdf",
    "title": "test.pdf",
    "region": "en-US",
    "spreadsheetLayout": {
        "ignorePrintArea": true,
        "orientation": "portrait",
        "fitToWidth": 0,
        "fitToHeight": 0,
        "scale": 100,
        "headings": false,
        "gridLines": false,
        "pageSize": {
            "width": "210mm",
            "height": "297mm"
        },
        "margins": {
            "left": "17.8mm",
            "right": "17.8mm",
            "top": "19.1mm",
            "bottom": "19.1mm"
        }
    }
})

convertFormatFromURL

从远程地址中进行文档格式转换

javascript
informat.storage.convertFormatFromURL(sourceURL,targetPath,setting)
参数类型描述
sourceURLString要转换的文档远程地址
targetPathString转换成功后目标文档路径
settingConvertSetting转换设置

下面是一个通过文档远程地址将docx格式文档转换为pdf格式的例子

js
informat.storage.convertFormatFromURL('https://xxx.xxx.xx/file/example.docx','table/field/example.pdf',{
    "filetype": "docx",
    "outputtype": "pdf",
    "title": "test.pdf",
})

convertFormatFromURL

从远程地址中进行文档格式转换

javascript
informat.storage.convertFormatFromURL(sourceURL, targetPath, onlyofficeServiceUrl, setting)
参数类型描述
sourceURLString要转换的文档远程地址
targetPathString转换成功后目标文档路径
onlyofficeServiceUrlStringonlyoffice服务地址,默认为管理后台配置的文件预览地址
settingConvertSetting转换设置

下面是一个通过文档远程地址将docx格式文档转换为pdf格式的例子

js
informat.storage.convertFormatFromURL('https://xxx.xxx.xx/file/example.docx','table/field/example.pdf',null,{
    "filetype": "docx",
    "outputtype": "pdf",
    "title": "test.pdf",
})

getContent

从共享存储中获取文件字符串内容

javascript
informat.storage.getContent(path)
参数类型描述
pathString共享存储路径
js
informat.storage.getContent('d97x4vygqxxt4/h9fdepr09gw2h/wddvtgi7md7x47du59sd0.png');

getBase64Content

从共享存储中获取文件base64编码内容

javascript
informat.storage.getBase64Content(path)
参数类型描述
pathString共享存储路径
js
informat.storage.getBase64Content('d97x4vygqxxt4/h9fdepr09gw2h/wddvtgi7md7x47du59sd0.png');