I'm trying to build an Electron-based app, mostly using code from my existing web app. The electron version connects to my server and often relies on that online content. I am using Ajax requests (using Jquery) handle things like the user logging on, and a php session is created, which is required to access most of the content.
I am now trying to get Javascript to automatically download a .zip file and save it to a location, without the user doing anything. I failed to do this using an Ajax request, so have tried to use the Node.js 'request' module. Then however, it wouldn't download the file because it was not authorised (because the request creates a new session, different to the existing, logged-in one).
How can I get something like the following to work?
const fs = require("fs");
...
$.ajax({
url: "my-server/file.zip",
success: function (data) {
fs.writeFile("local-file.zip", data);
}
});
Note - I think it failed due to some issue with the way the downloaded data is encoded, but don't understand exactly what the problem was.
Alternatively, is it possible to use the existing ajax session in the request module, and download it that way?
Related
Context
A feature that I would like to implement is to add a local(client side) database as offline feature for my cordova app using basic html, css, and js. It would be a fallback in case the internet connection is cut off. What I thought was that it would store the request url and request data inside a local JSON file and would have an option later on to be able to resend those failed request if the internet connection is restored.
Question
Is there a method to manipulate (add/create, edit/update, and delete) a JSON file stored inside a client app using pure JS?
What I've done so far is import and read a JSON file inside my app folder using the following code:
fetch("./database.json")
.then((response) => {
return response.json();
})
.then((data) => console.log(data));
(if there's a better way to do it, feel free to give a feedback in the comments)
That works fine and all, but I can't find any method on how to add/edit/delete an object and save the changes to that same JSON file.
According to this post which is half a decade ago, it is not possible without doing it in the backend.
When a popup shows up on my website, I want the div in that popup rendered one of many different ways depending on what a certain chosen setting is.
Ideally, I'd want a button pressed to open the popup, have the client ask the server for the code it needs based on what the current setting is, then once it gets the code from the server execute it and render the contents of the popup.
Do I require the code on the server, save the functions as properties on an object, and send the object to the client? Not sure if some of the functions would throw an error because DOM-related functions won't compile on a server?
Do I send the functions as a file and have the client require it on the client-side using a library like require.js or browserify?
I would use eval() to solve your problem.
Using Ajax on client :
$.ajax({
url:'MyServer/code.json',
success : function(data, code) {
eval(data.code);
}
});
MyServer/code.json
{
"code":"alert('Code from server executed.'); $('body').css({ 'background-color': '#000'});"
}
This is a simple example with JSON and this is not secure at all. Be carefull using eval().
I use a library that accepts a file as one of its parameters and it uses something like
<input type="file" id="file" name="files"/>
to get the needed file. Now, what i want is to send the file from my node.js server to the client via rest API like this:
$.get ('/getfile', function(data) {
reader = new FileReader();
reader.onload = function(theFile){
//some code here
};
reader.readAsText(f);
});
However, if i use something like this on the server side:
app.get('/getfile', function (req, res) {
var pathx = 'path to file';
res.download(pathx);
});
when it reaches the client, it does not see it as a file, rather the variable data, contains the contents of the file. how can i send a file down to the client so that the client can still see it as a file.
I can't post comment, so need to write an answer.
$.get call is a part of your client side code, right?
And you want to download file using jQuery.
Then there are 2 parts.
1. You need to have method on the server that serves file correctly (headers set up, etc). Open your browser and go to that address. What should happen is your browser downloads file. If it doesn't, you need to resolve this issue first.
Then you'd make your client make some calls using jQuery to download file. How to do this you can find in other answers like Download File Using jQuery
Now, can you say whether you have successfully completed step 1?
I am using the express module as the basis for my node.js server, and set up a static middleware as follows:
self.app.use(express.static(__dirname));
Within the root folder I have an html file that includes the following url to a php script on my remote server (completely different to the server hosting the node.js application) that returns jsonp data (having converted from xml data from the dataprovider):
var strURL = 'http://example.com/jsonp.php?callback=?&url=http://dataprovider.com/1.4/?arg1=xyz;arg2=abc';
And then a jquery getJSON call to actually get the json data:
$.getJSON(
strURL,
function (jsondata) {
// do some stuff with the json data
}
);
But when I load the html file that is being served from the static node.js folder, no data is returned... the code never reaches the jsondata function.
However, loading the very same html file placed on a "normal" server, the data is fetched just fine, and also if I load the strURL directly, the data is returned OK.
I suspect that this has something to do with cross domain issues, but for the life of me I can't get the page to work within the static node.js server using express. I've tried various solutions out there, but am now thoroughly confused and frustrated!
Any help would be welcome.
Have had cross domain issues with JSON in the past and got round them by using JSONP. There is a good explanation and tutorial at:
http://json-jsonp-tutorial.craic.com/index.html
I have a web application which clients are using.
Behind the scenes I'm generating logs on the clients' machine (because JavaScript or jQuery is running client side), but I'd like that log file to get to the server.
I don't have to use the input type as file.
Last note: I'd like to push this/these file(s) to the server without the user having to know about these files.
How can I do this? Is there any plugin I can use?
through javascript you can do form.submit() if the form contains a input type file then the file will get uploaded to the corresponding server folder.
Else you can also use Ajax to upload file using libraries like http://code.google.com/p/upload-at-click/
Best solution will be using the FileReader API
You cannot upload files from a client without the user first selecting the file. This is by design and cannot be bypassed.
Since you create the log files yourself (somehow), simply save a copy of the information that is posted to these files in a local variable or local storage of some sort, and send that instead. It's a simple AJAX call to do this...
function sendLog(logdata) {
$.ajax({
url: "savelog.php/aspx/whatever",
type: "post",
data: {
log: logdata
}
}).success(function() {
alert("it worked!!");
});
}
If you cannot save the log data to a local variable or storage then I don't see any way of doing this without user intervention.