Not able to downlad file using inappbrowser in Phonegap? - file

I Create an App where download a file from server in mobile, so I Use InAppBrowser plugin to open download page link, its working fine but i am not able to download file using my app, when i open download link in mobile browser its automatic start download.
please help me thanks in advance.

I think, its better to open system browser using inAppBrowser plugin for both the platform by using
window.open('http://apache.org', '_system', 'location=yes');
using this you can easily download.

InAppBrowser doesn't allow download. You will need to modify plugin to allow it for downloading.
For android, inside platforms\android\src\org\apache\cordova\inappbrowser
method name private void navigate(String url) {
include
this.inAppWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
cordova.getActivity().startActivity(i);
}
});
before this line this.inAppWebView.requestFocus();
again same code in the method public void run() {
after this segment
if (clearAllCache) {
CookieManager.getInstance().removeAllCookie();
} else if (clearSessionCache) {
CookieManager.getInstance().removeSessionCookie();
}
inAppWebView.loadUrl(url);
in your .java file inside onCreate
appView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Don't know about iOS

Related

codename one image chooser gives wrong path

I want to upload an image with codename one the issue is when i upload it i get the wrong image name and path , let me explain more so this is the code :
imaged.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
Display.getInstance().openImageGallery(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
if (ev != null && ev.getSource() != null) {
String filePath = (String) ev.getSource();
int fileNameIndex = filePath.lastIndexOf("/") +1;
String fileName = filePath.substring(fileNameIndex);
System.out.println("image : "+ fileName);
}
}
});
}
});
c3.add(imaged);
i want to get the image name to insert it into my data base the lint that i gott is :
C:/Users/Emel/AppData/Local/Temp/temp8005230168902905005..png
the image which i'm selecting is hosted on my wamp server under the www folder:
http://localhost/PiWeb1/TeamFlags/es.png
which is wrong even the file name is wrong how can i get the real path and file name!
This is a bit problematic on devices. They are very inconsistent when it comes file system/media so we try to replicate some of those problems in the simulator by copying the selection to the side.
The reason is simple, say you had access to the image directory of the phone you could read and upload all the images without the users consent. So they OS copies an image and gives you access only to that data.
You might have better luck with this on devices, if not you might have better luck with: https://www.codenameone.com/blog/native-file-open-dialogs.html
As a side note your URL won't be an http URL it will be a file URL.

Configuring Webpack build for file:// use in CEF

I have to develop a webapp for a CEF-Browser environment. There is no HTTP server available, everything will be served over file:// protocol.
When developing a Webapp nowadays one doesn't get round working with a framework like react/vue for frontend. The standard webpack build scripts of those build a bundle which only works served over HTTP.
Is it possible to configure webpacks build bundle to work on file:// or is there another way to use react or vue via file://?
I'm suggest read CEF wiki more carefully. You are especially interested in https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md#markdown-header-request-handling
In short:
You can register custom scheme handler to serve resources over http+custom fake domain.
You can pack resources in zip for example if you like, or leave them at file system as is (but in that case you can expect that some funny users can edit your files, and then report back unexisting errors back to you).
Important helpers already done (but you can write own when need.)
You can... many other things.
Main thing that "file" scheme are more restricted, and for example you can't do XHR requests. But for custom handler you can. Even if dynamic loader for some reason use XHR instead DOM-based loading it will work again same as on http without touching network.
cefclient itself also has usage of custom schemes. Check URL of Tests->Other... in menu. :)
PS: Sorry that my answer doesnt have direct answer for your question. But, custom resource handling in CEF is so common, that i'm just should say about.
fddima is right - you don't need to configure your webpack (although it would be theoretically possible). Instead you can use custom scheme handler in CEF. I made it work with angular at work.
I wrote blog post on how to serve web application via 'file' protocol in CEF.
What you want to add is your scheme handler and its factory:
using System;
using System.IO;
using CefSharp;
namespace MyProject.CustomProtocol
{
public class CustomProtocolSchemeHandler : ResourceHandler
{
// Specifies where you bundled app resides.
// Basically path to your index.html
private string frontendFolderPath;
public CustomProtocolSchemeHandler()
{
frontendFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./bundle/");
}
// Process request and craft response.
public override bool ProcessRequestAsync(IRequest request, ICallback callback)
{
var uri = new Uri(request.Url);
var fileName = uri.AbsolutePath;
var requestedFilePath = frontendFolderPath + fileName;
if (File.Exists(requestedFilePath))
{
byte[] bytes = File.ReadAllBytes(requestedFilePath);
Stream = new MemoryStream(bytes);
var fileExtension = Path.GetExtension(fileName);
MimeType = GetMimeType(fileExtension);
callback.Continue();
return true;
}
callback.Dispose();
return false;
}
}
public class CustomProtocolSchemeHandlerFactory : ISchemeHandlerFactory
{
public const string SchemeName = "customFileProtocol";
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
return new CustomProtocolSchemeHandler();
}
}
}
And then register it before calling Cef.Initialize:
var settings = new CefSettings
{
BrowserSubprocessPath = GetCefExecutablePath()
};
settings.RegisterScheme(new CefCustomScheme
{
SchemeName = CustomProtocolSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CustomProtocolSchemeHandlerFactory()
});

download excel file using htmlunit

I am trying to download xls file from a website using Htmlunit. When I click the link to download the file, I get a javascript confirm box.
I tried multiple options but not able to find the slution:
ConfirmHandler okHandler = new ConfirmHandler(){
#Override
public boolean handleConfirm(Page arg0, String arg1) {
// TODO Auto-generated method stub
return true;
}
};
webClient.setConfirmHandler(okHandler);
Also tried with
final String page = currentPage.getElementById("downloadExcel").click().getWebResponse().getContentAsString();
String buttonJavaScript = "window.location='...........'";
ScriptResult result = page.executeJavaScript(buttonJavaScript);
webClient.waitForBackgroundJavaScript(1000);
It would be really helpful if some one can suggest the solution for this.

How to write logs data in a local txt file?

I have an AIR-app and I want it to write down logs to a txt file in its application directory.
I have this class:
public class Logger
{
public static function log(message:String):void
{
var logFile:File = File.applicationDirectory;
logFile = logFile.resolvePath("log/Logs.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(logFile, FileMode.APPEND);
fileStream.writeUTFBytes("\n" + message);
fileStream.close();
}
}
}
After installation my app has this file structure
log
--Logs.txt
META-INF
xml
airapp.exe
airapp.swf
mimetype
So the path I'm using is correct.
But no writes happen to the file!
Could you help me with that please?
Thanks
Upd. Ok, I'm stupid. I should have read documentation properly first.
You cannot edit data in application directory.
So I should use applicationStorageDirectory.
But how can I create a file in that directory once (on app installation) and then append logs in it?
Try the following....
function saveLog(logStr:String):void
{
var myFile:File = File.applicationStorageDirectory.resolvePath("my_logs.txt");
var fs:FileStream = new FileStream();
if(!myFile.exists){
fs.open(myFile, FileMode.WRITE);
}
else{
fs.open(myFile, FileMode.APPEND);
}
fs.writeUTFBytes("\n" + logStr);
fs.close();
}
call this function on creation complete and then from where to add logs.
If you want the real log than you should use third party installer rather than native run time of adobe air. Try Wixedit(opensource) or install aware .
You have to export your project into captive run time format that is provided in flash builder.
try this link
http://www.adobe.com/devnet/air/articles/customize-setup-for-AIR-app-with-captive-runtime.html
Try something like:
function saveLog(content:String):void
{
var data:ByteArray = new ByteArray();
data.writeMultiByte(content, "utf-8");
file.save(data, "Logs.txt");
}
saveLog('testing :)');
To open and edit your log file, try to follow this tutorial

Tabris, Eclipse RAP: Is there a public API to get to know which client has requested the page?

I am writing a RAP application using Eclipse RAP. The client may be any brower, an iPad or an android tablet (using Tabris). Is there any chance to find out which client has sent a request?
The background for my question is: Tabris does not support SashForms until now. For this reason, I want to render a SashFrom in case I am serving a web client but don't want to create a SashForm if serving the android client. I could do something like this:
public static boolean isAndroid() {
return getUserAgent().contains(Constants.ID_ANDROID);
}
private static String getUserAgent() {
return RWT.getRequest().getHeader(Constants.USER_AGENT);
}
public static boolean isIos() {
return getUserAgent().contains(Constants.ID_IOS);
}
public static boolean isWeb() {
return !isAndroid() && !isIos();
}
But I'd like to avoid this approach, because it uses internal API and since I am using standalone RAP I need to add a the servlet-api.jar to WEB-INF/lib folder to get this running, which is also not very nice.
Thanks in advance for your help and information,
Tobias.
I think this can help: https://github.com/eclipsesource/tabris/blob/master/com.eclipsesource.tabris/src/com/eclipsesource/tabris/ClientDevice.java
One alternative approach instead of using ClientDevice is to use RWT.getClient().
if( RWT.getClient() instanceof WebClient ) {
....
}
This is sometimes useful because ClientDevice can be null if the web client accesses the application. It's a client service that is only valid for the mobile client atm.

Resources