mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(file-system): append, appendText & createFile (#10285)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils } from '@nativescript/core';
|
||||
import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils, Screen, Http, AndroidDirectory, ImageSource, alert } from '@nativescript/core';
|
||||
|
||||
let page: Page;
|
||||
|
||||
@@ -217,3 +217,103 @@ function getFileNameFromContent(content: string) {
|
||||
const file = getFileAccess().getFile(content);
|
||||
return decodeURIComponent(file.name).split('/').pop().toLowerCase();
|
||||
}
|
||||
|
||||
let lastDownload: File;
|
||||
export function createFileInDownloads() {
|
||||
if (!global.isAndroid) {
|
||||
return;
|
||||
}
|
||||
const width = Screen.mainScreen.widthPixels;
|
||||
const height = Screen.mainScreen.heightPixels;
|
||||
const randomImageUrl = `https://picsum.photos/${width}/${height}.jpg?random=${Date.now()}`;
|
||||
|
||||
Http.getFile(randomImageUrl).then((result: File) => {
|
||||
let file = File.android.createFile({
|
||||
directory: AndroidDirectory.DOWNLOADS,
|
||||
name: `${Date.now()}.jpg`,
|
||||
mime: 'image/jpeg',
|
||||
relativePath: `NativeScript`,
|
||||
});
|
||||
result
|
||||
.copy(file.path)
|
||||
.then((done) => {
|
||||
lastDownload = file;
|
||||
console.log('done: ' + done + '\n' + 'Original path: ' + result.path + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n');
|
||||
alert(`File saved in ${AndroidDirectory.DOWNLOADS}/NativeScript`);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function createFileInGallery() {
|
||||
if (!global.isAndroid) {
|
||||
return;
|
||||
}
|
||||
const width = Screen.mainScreen.widthPixels;
|
||||
const height = Screen.mainScreen.heightPixels;
|
||||
const randomImageUrl = `https://picsum.photos/${width}/${height}.jpg?random=${Date.now()}`;
|
||||
|
||||
Http.getFile(randomImageUrl).then((result: File) => {
|
||||
let file = File.android.createFile({
|
||||
directory: AndroidDirectory.PICTURES,
|
||||
name: `${Date.now()}.jpg`,
|
||||
mime: 'image/jpeg',
|
||||
relativePath: `NativeScript`,
|
||||
});
|
||||
result
|
||||
.copy(file.path)
|
||||
.then((done) => {
|
||||
console.log('done: ' + done + '\n' + 'Original path: ' + result + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n');
|
||||
alert(`File saved in ${AndroidDirectory.PICTURES}/NativeScript`);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function createFileInMusic() {
|
||||
if (!global.isAndroid) {
|
||||
return;
|
||||
}
|
||||
|
||||
Http.getFile('https://github.com/rafaelreis-hotmart/Audio-Sample-files/raw/master/sample.mp3').then((result: File) => {
|
||||
let file = File.android.createFile({
|
||||
directory: AndroidDirectory.MUSIC,
|
||||
name: `${Date.now()}.MP3`,
|
||||
mime: 'audio/mp3',
|
||||
relativePath: `NativeScript/MP3`,
|
||||
});
|
||||
|
||||
result
|
||||
.copy(file.path)
|
||||
.then((done) => {
|
||||
console.log('done: ' + done + '\n' + 'Original path: ' + result + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n');
|
||||
|
||||
alert(`File saved in ${AndroidDirectory.MUSIC}/NativeScript/MP3`);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function createAndAppendText() {
|
||||
const file = File.fromPath(path.join(knownFolders.temp().path, `${Date.now()}-app.txt`));
|
||||
file.appendTextSync('Hello');
|
||||
file.appendTextSync(' World');
|
||||
const data = file.readTextSync();
|
||||
console.log('createAndAppend:', data === 'Hello World');
|
||||
}
|
||||
|
||||
export function createAndAppendData() {
|
||||
const file = File.fromPath(path.join(knownFolders.temp().path, `${Date.now()}-app.txt`));
|
||||
const hello = global.isIOS ? NSString.stringWithString('Hello').dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String('Hello').getBytes('UTF-8');
|
||||
const world = global.isIOS ? NSString.stringWithString(' World').dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String(' World').getBytes('UTF-8');
|
||||
file.appendSync(hello);
|
||||
file.appendSync(world);
|
||||
const data = file.readTextSync();
|
||||
console.log('createAndAppendData:', data === 'Hello World');
|
||||
}
|
||||
|
||||
@@ -5,5 +5,10 @@
|
||||
<Button text="Pick File" tap="pickFile" />
|
||||
<Button text="Pick Multiple Files" tap="pickFiles" />
|
||||
<Button text="Test Copy" tap="copyTest" />
|
||||
<Button text="External File Creation" tap="createFileInDownloads" />
|
||||
<Button text="Add file to gallery" tap="createFileInGallery" />
|
||||
<Button text="Add file to music" tap="createFileInMusic" />
|
||||
<Button text="Append Text" tap="createAndAppendText" />
|
||||
<Button text="Append Data" tap="createAndAppendData" />
|
||||
</StackLayout>
|
||||
</Page>
|
||||
|
||||
Reference in New Issue
Block a user