i am trying to post an image to a a backend server that is an Express Server.
I am using cordova file transfer(installed through cordova plugin add cordova-plugin-file-transfer )
I have imported the file transfer like this:
import {Transfer} from 'ionic-native';
here is my component that posts the file to the server
save() {
base64Image = open("/Users/user1/1.jpg");
let ft = new Transfer();
let filename = "example" + ".jpg";
let options = {
fileKey: 'file',
fileName: filename,
mimeType: 'image/jpeg',
chunkedMode: false,
headers: {
'Content-Type' : undefined
},
params: {
fileName: filename
}
};
ft.upload(base64Image, "http://localhost:3500/api/v1/file", options, false);
}
the error i get whenever i call the save function is:
FileTransfer is not defined
help will be appreciated
Install with ionic since you are using ionic-native.
ionic plugin add cordova-plugin-file-transfer --save.
The save option is to ensure there is an entry in config.xml.
Also call any plugin within
platform.ready().then(()=>{})
Plugins are loaded after the app is loaded.
UPDATE:
Cordova is not supported and most plugins will not load with ionic serve command.
You need to run it in an emulator or a device.
Related
I have a bot command which is an image scraper, and it uses puppeteer. I have the puppeteer files downloaded into my VSC(Visual Studio Code) and when I run the bot from the VSC terminal, the image scraper function works. I can commit the files that I use onto GitHub, which is then linked to Heroku. But when I try to host the bot on Heroku and use the image scraper command, Heroku gives UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! I have also added the puppeteer buildpack to my heroku project as well, and that doesn't seem to fix the problem. One solution I've seen is adding { args: ['--no-sandbox'] } to the code, but I'm not sure where to add it. Where can I add the --no-sandbox, or is there another fix to this problem? Thanks
Image Scraper code:
var Scraper = require('images-scraper');
const google = new Scraper({
puppeteer: {
headless: true
}
})
module.exports = {
name: 'image',
description: 'sends img to channel',
async execute(client, message, args){
const image_query = args.join(' ');
let rng = Math.round(Math.random()*10)
if(!image_query) return message.channel.send('Unable to find image');
const image_results = await google.scrape(image_query, 100);
message.channel.send(image_results[rng].url);
}
}
Edit (for image-scraper)
You can use all puppeteer.launch options as normal, with the puppeteer option. You can do it like this.
const google = new Scraper({
puppeteer: {
headless: true,
args: ["--no-sandbox"],
},
});
You can read more about using image-scraper on Heroku here.
Previous (for puppeteer)
As you mentioned you can add --no-sandbox argument to your code like this.
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
],
});
You can read the puppeteer.launch documentation here.
If this doesn't help you, you should check out the official troubleshooting guide on running Puppeteer on Heroku.
I have the #pdftron node module installed in a test Node (Hapi) application on my Mac. I'm trying to generate a PDF file from a HTML string but the saved file is 0 KB. I've tried this two ways:
By implementing the example code in the GET handler for a route
configured in index.js
By running the sample scripts that are
installed at
node_modules/#pdftron/pdfnet-node/samples/HTML2PDFTest/NODEJS/HTML2PDFTest.js
In both cases, any new PDFs that are saved have a size of 0KB and any existing PDFs that are supposed to be modified by the process remain unchanged.
I've checked that the html2pdf module library path is being set correctly.
The route handler code is as follows, which is in accordance with example code provided by PDFTRon.
server.route({
method: 'GET',
path: '/pdftron/html',
handler: (request, h) => {
const { PDFNet } = require('#pdftron/pdfnet-node/lib/pdfnet.js')
const main = async () => {
await PDFNet.initialize()
await PDFNet.HTML2PDF.setModulePath('node_modules/#pdftron/pdfnet-node/lib')
const output_path = '/tmp/'
try {
const html2pdf = await PDFNet.HTML2PDF.create();
const doc = await PDFNet.PDFDoc.create();
const html = '<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>'
html2pdf.insertFromHtmlString(html);
await html2pdf.convert(doc);
doc.save(output_path.concat('pdf2html.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
} catch (err) {
console.log(err)
}
}
PDFNet.runWithCleanup(main, 0).then(function () { PDFNet.shutdown(); })
return 'PDF2HTML Test'
}
})
Any thoughts/suggestions would be much appreciated.
Update on this - I had feedback from PDFTron to say that there is currently an issue with the current stable release of the Mac OS build and that they are addressing it.
In the meantime, they pointed me to the nightly experimental builds here. I downloaded and installed a recent experimental build and the PDF files now save OK using that.
I'm trying to upload an image via nativescript-background-http plugin.
this my code:
var session = bghttp.session("image-upload");
var request = {
url: 'http://localhost:4200/',
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": imageFile.name
},
description: "{ 'uploading': " + imageFile.name + " }"
};
var task = this.session.uploadFile(path, request);
when I try to build my app, it will show the error:
TypeError: Cannot set property 'NAMESPACE' of undefined
The line of error is exactly this line:
var task = this.session.uploadFile(path, request);
any idea?
I've got a solution for you. To upload image using nativescript background http is now easy using a file stream method on your backend server.
https://www.ekpotoliberty.com/read/NativeScript-Image-Uploading-to-NodeJs-Api-+-Cloudinary-using-NativeScript-Background-HTTP/5dad52c76dc813685f8da26f
Follow the link above this will help you get through for nativescript core.
I am using of some of the npm modules which are making get request behind the scenes to pull some data from websites. But there is no option or setting to set proxy for those requests, so I want to know how to set proxy for entire electron app so that all the requests go through that proxy?
Using request:
Use environment variables :
process.env.HTTP_PROXY = 'http://192.168.0.36:3128'
Using Axios:
Install this package :
npm install https-proxy-agent
Then :
const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');
let config = {}
config.httpsAgent = new HttpsProxyAgent('http://192.168.0.36:3128')
config.url = 'https://example.com'
config.method = 'GET'
axios(config).then(...).catch(...)
Electron app
For the wall app (like IMG SRC in HTML), you can use command line switches supported by Electron :
const { app } = require('electron')
app.commandLine.appendSwitch('proxy-server', '172.17.0.2:3128')
app.on('ready', () => {
// Your code here
})
See documentation
I have the following code to create a file from a Firefox Add-on SDK extension.
panel.port.on("mbData", function(data) {
console.log("Recebi dados. Data: " + data);
OS.File.writeAtomic("mb.txt", data, {write: true, create: true}).then(function(aResult) {
console.log("Criei o ficheiro\n");
}, function(ex) {
console.log("Error!\n"+ex);
});
});
The code above works great when I run using jpm run. But, when I create the xpi file (jpm xpi) and install it on Firefox it doesn't work. It seems that the file it's not being created. In addition, I can't access any log files.
Am I doing anything wrong here?
try to use this line:
Components.utils.import('resource://gre/modules/osfile.jsm');
Before to call the writeAthomic method.