I am attempting to use a javascript function to load a local file in my WP7 project. So far the following code works but it loads an image file from a web address instead of a local directory in my WP7 application ( Resources/Icons/appbar.go.rest.png). I have researched everywher to load a local image file properly from a local directory in my project but have yet to be successful. My code is as follows
Javascript
var body = document.getElementsByTagName("body")[0];
var s = document.createElement("input");
/loads image file from web address
s.src = "http://www.gravatar.com/avatar/a4d1ef03af32c9db6ee014c3eb11bdf6? s=32&d=identicon&r=PG";
s.type = "image";
body.appendChild(s);
As I said the above code works, but I would like to use a local image file in my project rather than the web address above. Does anyone know how to correctly implement this solution?
you can read this article which provide a solution by using WebClient:
Operating with image files in a Windows Phone 7 application
http://dotnet.dzone.com/articles/operating-image-files-windows
jQuery .load() method can load files well, but unfortunately I haven't used it on windows phone 7 environment which saves files on its Local Storage.
http://api.jquery.com/load/
You can't load the image directly from the clien't machine (remember that that javascript peace of code will run on the client's local machine). You should serve the image file as you are serving the Javascript file, then point 'src' there.
Related
I am trying to build a Chrome extension but I have encountered a problem. I need access to a file that is in the same directory as my JS script. Here is the representation:
my_script.js
My Folder
|-> my_file.file
I use a third party library that does some required things with it. And I need to pass in the path to my_file.file The problem is my_script.js gets injected into the website I am trying to change and the code instead of searching on the local file system it tries to find it in www.website.com/My Folder/my_file.file which obviously does not exist. How can I make it so that it seacrhes relative to my_script.js? I unfortunately can't pass the file itself to func(), it has to be the path of the file. The file I am trying to reach is about 200MB which I was planning on shipping with my extension. Also this project is vanilla JS. Thank you!
my_script.js
...
lib.func('My Folder/my_file.file').then(function (out) {
document.out = out;
});
...
I want to display an image file from a local folder in JavaScript (p5.js). Every time I try I get: Access to image at "XXXXX" from origin "null" has been block by CORS policy: The response is invalid.
The HTML, js and images are all inside a file tree, inside the media server's project folder.
They HAVE to be run locally. Running from any other location than the folder they are in is not an option (referring to a lot of answers talking about running a web server.
function setup() {
createCanvas(96, 192);
img = loadImage('images/square1-01.png'); // Load the image
}
function draw() {
image(img, 0, 0);
}
//ALSO TRIED
img = loadImage('file://images/square1-01.png'); // Load the image
The above text is a super simplified version that I isolated from the main file so I could run it standalone, just so I could get to the point of getting an image, before I started adding in the other layers of complexity.
Thus the layout is entirely from a tutorial.
Relevant files are:
.../web/d3test.html
.../web/d3test.js
.../web/images/square1-01
.../web/images/square2-01
.../web/images/square3-01
JavaScript is blocked from accessing local files like this, for security reasons. You wouldn't want every website snooping through the files on your hard drive. That's what's causing your error.
Instead of accessing files on your local hard drive, you need to access files from a web server. You could upload your your sketch (along with your images) to a web host. Shameless self-promotion: here is a tutorial that introduces some options for hosting.
You could also use the P5.js editor. This is a free editor for P5.js that automatically uploads everything to a web host. Your could should work fine there.
You could also run your own local web server.
If you really, really, really need to run this without a web server, then you can try the File API described in this answer or find a setting in your browser described in this answer.
I am building an HTML5 phonegap application. This app exports data so that the user can backup and restore any time. I'm doing this exporting with the following javascript code:
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(this.data, null, "\t"));
var dlAnchorElem = document.createElement('a');
dlAnchorElem.setAttribute("href", dataStr);
dlAnchorElem.setAttribute("download", "data.json");
document.body.appendChild(dlAnchorElem);
dlAnchorElem.click();
This generates an anchor tag with an encoded file and clicks so it downloads. Works great on browser, but does nothing in a compiled Cordova application.
After doing some research, I found that the default solution would be to use a download plugin for Cordova, specifically this one: https://github.com/apache/cordova-plugin-file-transfer
I read the documentation, but it does not seem to take an encoded file as parameter, but an encoded URL for download. Also, it takes the save path on the phone, which I prefer would just default to the download folder.
My question is: What is the best way to achieve this, considering I'm dynamically generating the JSON backup file. Is there perhaps an AndroidManifest directive that allows for file downloads?
After some research and trying many different hacks, I came to the conclusion that it's currently not allowed natively with cordova or with the available plugins. My solution was to, instead of writing to the filesystem, use the web share api to let the user export the way he finds best (including file, if he chooses dropbox, onedrive or google drive).
I'm new to javascript might be this question looks silly. How to check whether the html files are loading from local file system or server using javascript?
For example when I open the html files from my local system (/home/user/1.html) browser shows in url
file:///home/user/1.html
But if i load 1.html file into my local server then, if i access that file browser shows in url like below
http://localhost/GUI/1.html
I want to find whether files are loading from my server or local file system using java script.
Is there any way to find this using java script method.
You can use the window.location object to do that.
use window.location.protocol property.
The following line of code will return true if the file is being served from the file system.
window.location.protocol == "file:";
It basically checks if the protocol being used is file or not.
I created a html page (stored locally) that uses Googlemaps API. My whole page is a basic google map with some customization. I want to take a screenshot every time I change some parameters in the customization so later I can easily compare.
I've found this similar answer and I got it to work on my machine. However, when I change the url from an actual webpage into my own local html file, Phantomjs only saves an entirely black image.
Here is my script.
var WebPage = require('webpage');
page = WebPage.create();
page.open('googlemaps_demo.html');
page.onLoadFinished = function() {
page.render('myScreenShot' + '.png');
phantom.exit();}
The file googlemaps_demo.html and this js script itself are in the same folder. Could someone explain to me why this code only works for an online url, but not a local html file? And how to fix it?
You probably need to specify file using a file:/// scheme and a full location of your file, e.g. file:///c:/local/page.htm