本文整理汇总了TypeScript中fs.readFile函数的典型用法代码示例。如果您正苦于以下问题:TypeScript readFile函数的具体用法?TypeScript readFile怎么用?TypeScript readFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了readFile函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
router.post('/submit', function(req, res) {
// extract user id of creator/owner of comic from request header
var authorID = req.cookies._id;
var authorUsername = req.cookies.userName;
var publicationDate;
// get values of comic data fields
var title = req.body.title;
var description = req.body.description;
var genre = req.body.genre;
var toPublish;
var votedPpl: [{ id: String, votetype: Number }] = [{ id: "", votetype: 0 }];
var openToContribution;
var openToCommenting;
var thumbnailID = "";
var upvotes: Number = 0;
var fave: [String] = [""];
var selectedImageID = req.body.selectedImageID;
if (req.body.openToContribution == "on"){
openToContribution = true;
} else {
openToContribution = false;
}
if (req.body.openToCommenting == "on"){
openToCommenting = true;
} else {
openToCommenting = false;
}
//set the toPublish field relative to which submit button is pushed
if (req.body.submit == "draft"){
toPublish = false;
} else if(req.body.submit == "publish"){
toPublish = true;
}
var c = new Comic.Comic(req.mongoose);
var cc = new ComicCell.ComicCell(req.mongoose);
if(req.file){
fs.readFile(req.file.path, function (err, img) {
c.insert(title, authorID, authorUsername, description, genre, toPublish, openToContribution, openToCommenting, thumbnailID, upvotes,votedPpl,fave, (comicID: String): void => {
// read the image file passed in the request and save it
cc.insert(comicID, authorID, authorID, toPublish, (imgName: String): void=> {
// If there's an error
if (!imgName) {
console.log("There was an error")
res.redirect("./create");
res.end();
} else {
//var newPath = "./uploads/fullsize/" + imgName;
c.update(comicID, title, authorID, authorUsername, publicationDate, description, genre, toPublish, openToContribution, openToCommenting, imgName, upvotes,votedPpl,fave, (): void => {});
var newPath = "./uploads/fullsize/" + imgName;
//var imageList = [(req.headers['host'] + "/webcomic/image/" + imgName)];
// write image file to uploads/fullsize folder
fs.writeFile(newPath, img, function (err) {
if (err)
return console.error(err);
//redirect to the newly created comic
res.redirect('./id/' + comicID);
});
easyimg.rescrop({
src: newPath, dst:'./uploads/thumbnails/' + imgName,
width:128, height:128,
//cropwidth:128, cropheight:128,
x:0, y:0
}).then(
function(image) {
console.log('Resized and cropped: ' + image.width + ' x ' + image.height);
},
function (err) {
console.log(err);
}
);
}
var db = req.db;
var contributors = db.get('contributors');
contributors.update({ guid: ObjectId(req.cookies._id) }, {
$addToSet: {
comicIDs: [comicID]
}
});
});
});
});
}else{
var db = req.db;
var comicCells = db.get('comiccells');
c.insert( title,
authorID,
authorUsername,
description,
genre,
toPublish,
openToContribution,
openToCommenting,
selectedImageID,
upvotes,
//.........这里部分代码省略.........
示例2: Promise
return new Promise((resolve, reject) => {
fs.readFile(file, (err, res) => {
if (err) return reject(err);
return resolve(res)
})
})
示例3: constructor
fs.writeFile("Harry Potter",
"\"You be wizzing, Harry,\" jived Dumbledore.",
{
encoding: "ascii"
},
assert.ifError);
var content: string,
buffer: Buffer;
content = fs.readFileSync('testfile', 'utf8');
content = fs.readFileSync('testfile', {encoding : 'utf8'});
buffer = fs.readFileSync('testfile');
buffer = fs.readFileSync('testfile', {flag : 'r'});
fs.readFile('testfile', 'utf8', (err, data) => content = data);
fs.readFile('testfile', {encoding : 'utf8'}, (err, data) => content = data);
fs.readFile('testfile', (err, data) => buffer = data);
fs.readFile('testfile', {flag : 'r'}, (err, data) => buffer = data);
class Networker extends events.EventEmitter {
constructor() {
super();
this.emit("mingling");
}
}
var errno: string;
fs.readFile('testfile', (err, data) => {
if (err && err.errno) {
示例4: readImageFile
static readImageFile(file: string, callback: any, thisArg?: any) {
const fileNameWithoutExt = getFileNameWithoutExtension(file);
const testFileNameForJavaScriptIdentifier = /^[(\d+)`~\| !@#\$%\^&\*\(\)\-=\+\?\.,<>]+|[`~\|!@#\$%\^&\*\(\)\-=\+\? \.,<>]/g;
if (testFileNameForJavaScriptIdentifier.test(fileNameWithoutExt)) {
callback.call(
thisArg,
new Error(
'Incorrect file name ' + fileNameWithoutExt + ' (file: ' + file + ')',
),
null,
);
}
if (!ImageHelper.isImageFileSupported(file)) {
callback.call(
thisArg,
new Error(
'Supported files: *.' +
supportedImageExtensions.join(', *.') +
'. File ' +
file +
' is not supported.',
),
null,
);
}
switch (getExtension(file).toUpperCase()) {
case 'JPEG':
case 'JPG':
fs.readFile(file, function(error: any, data: any) {
if (error) {
callback.call(
thisArg,
new Error("FS: Can't read file " + file + ', error: ' + error),
null,
);
return;
}
// read jpeg
let textureJpeg;
try {
textureJpeg = jpegEngine.decode(data);
} catch (e) {
callback.call(
thisArg,
new Error("JPG: Can't decode file " + file + ', error: ' + e),
null,
);
return;
}
// create png
const texturePng = new pngEngine({
filterType: 0,
width: textureJpeg.width,
height: textureJpeg.height,
});
// convert data from jpg_plugin (rgb) to png_plugin (rgb)
for (let i = 0; i < textureJpeg.data.length; i += 4) {
texturePng.data[i] = textureJpeg.data[i];
texturePng.data[i + 1] = textureJpeg.data[i + 1];
texturePng.data[i + 2] = textureJpeg.data[i + 2];
texturePng.data[i + 3] = textureJpeg.data[i + 3];
}
callback.call(thisArg, null, texturePng);
});
break;
case 'PNG':
fs.createReadStream(file)
.pipe(
new pngEngine({
filterType: 0,
}),
)
.on('parsed', function(this: any) {
callback.call(thisArg, null, this);
})
.on('error', function(error: any) {
callback.call(
thisArg,
new Error("PNG: Can't decode file " + file + ', error: ' + error),
null,
);
});
break;
case 'BMP':
fs.readFile(file, function(error: any, data: any) {
if (error) {
callback.call(
thisArg,
new Error(
"File system error: Can't read file " +
file +
', error: ' +
//.........这里部分代码省略.........
示例5:
.reply(201, (uri: string, requestBody: string, cb: nock.ReplyCallback) => {
fs.readFile('cat-poems.txt' , cb); // Error-first callback
});
示例6: fail
return new Promise<string>((ok, fail) => {
fs.readFile(filePath, (err, data) => err ? fail(err) : ok(data.toString()));
});
示例7: init
init(context: any, callback: any) {
if (!this.path) {
throw new Error('"path" is required');
}
this.path = this.resolvePath(this.path);
callback();
},
read(context: any, callback: any) {
const path = this.path;
fs.readFile(path, {encoding: 'utf8'}, (err, src) => {
if (err) {
return callback(err);
}
// myCompiler.compile(src, callback);
});
},
getSourceFile() {
return this.path;
}
});
// $ExpectType void
dependencies.registerStyleSheetType('my-custom-type', {});
// $ExpectType void
dependencies.registerPackageType('dir', {
示例8: require
var xml_manipulate =
require ("/var/www/data_base/common/typescript_common/xml_manipulate");
// ------------------------------------------------------------------
console.log ("*** 開始 ***");
var file_xml:string = process.argv[2];
var id_in:string = process.argv[3];
var population_in:number = process.argv[4];
console.log (id_in + "\t" + population_in);
var parser = new xml2js.Parser();
fs.readFile(file_xml, function(err, data) {
console.log ("*** start reading ***");
parser.parseString(data);
});
parser.on('end', function(result) {
var dict_aa = text_manipulate.dict_update_proc
(result,id_in,population_in);
var out_str = xml_manipulate.dict_to_xml_proc (dict_aa);
fs.writeFile(file_xml,out_str);
console.log ("*** 終了 ***");
});
// ------------------------------------------------------------------
示例9:
var stringEncoding: string | null = 'utf8';
content = fs.readFileSync('testfile', 'utf8');
content = fs.readFileSync('testfile', { encoding: 'utf8' });
stringOrBuffer = fs.readFileSync('testfile', stringEncoding);
stringOrBuffer = fs.readFileSync('testfile', { encoding: stringEncoding });
buffer = fs.readFileSync('testfile');
buffer = fs.readFileSync('testfile', null);
buffer = fs.readFileSync('testfile', { encoding: null });
stringOrBuffer = fs.readFileSync('testfile', nullEncoding);
stringOrBuffer = fs.readFileSync('testfile', { encoding: nullEncoding });
buffer = fs.readFileSync('testfile', { flag: 'r' });
fs.readFile('testfile', 'utf8', (err, data) => content = data);
fs.readFile('testfile', { encoding: 'utf8' }, (err, data) => content = data);
fs.readFile('testfile', stringEncoding, (err, data) => stringOrBuffer = data);
fs.readFile('testfile', { encoding: stringEncoding }, (err, data) => stringOrBuffer = data);
fs.readFile('testfile', (err, data) => buffer = data);
fs.readFile('testfile', null, (err, data) => buffer = data);
fs.readFile('testfile', { encoding: null }, (err, data) => buffer = data);
fs.readFile('testfile', nullEncoding, (err, data) => stringOrBuffer = data);
fs.readFile('testfile', { encoding: nullEncoding }, (err, data) => stringOrBuffer = data);
fs.readFile('testfile', { flag: 'r' }, (err, data) => buffer = data);
}
{
var errno: number;
示例10:
/// <reference path="../../typings/node/node.d.ts" />
import * as fs from 'fs';
const filename = '../public/target.txt';
fs.readFile(filename, (err, data) => {
if(err) { throw err; }
console.log(data.toString());
});