Nhảy tới nội dung

my.saveFile

saveFile

my.saveFile là API dùng để lưu lại file về thiết bị từ 1 đường dẫn tạm thời (local temporary file)

Quét mã để trải nghiệm

Xem code mẫu trên Tini Studio

Demo

Trải nghiệm thử với trình giả lập bên dưới

API Params

Thuộc tínhKiểu dữ liệuBắt buộcMô tả
filePathStringĐường dẫn của local temporary file
successFunctionCallback function khi save file được thực hiện thành công
failFunctionCallback function khi save file thất bại
completeFunctionCallback function khi việc save file kết thúc cho dù thành công hay thất bại.

Callback success function payload

Thuộc tínhKiểu dữ liệuMô tả
filePathStringĐường dẫn của tới file đã saved

Sample Code

<view>
<block-header title="Usage" description="File system" />
<view class="block-content">
<image src="{{tempFilePath}}" />
<button class="button-full" onTap="onChooseImage">Choose Image</button>
</view>
<view class="block-content">
<button class="button-full" onTap="onSaveFile">Save Image</button>
<button class="button-full" onTap="onGetFileInfo">Get File Info</button>
<button class="button-full" onTap="onGetSavedFileInfo">Get Saved File Info</button>
<button class="button-full" onTap="onGetSavedFileList">Get Saved File List</button>
<button class="button-full" onTap="onRemoveSavedFile">Remove Save File</button>
</view>
</view>
Page({
data: {
tempFilePath: undefined,
savedFilePath: undefined
},
onChooseImage() {
my.chooseImage({
count: 1,
success: (res) => {
console.log(res);
this.setData({
tempFilePath: res.filePaths[0]
});
},
fail: (e) => {
console.log(e);
}
});
},
onSaveFile() {
my.saveFile({
filePath: this.data.tempFilePath,
success: (res) => {
console.log(res);
my.alert({
title: 'Saved',
content: `File path ${res.filePath}`
});
this.setData({
savedFilePath: res.filePath
});
},
fail: (e) => {
console.log(e);
}
});
}
});