Temporary file name with js - javascript

Is it possible to get temporary filename with javascript?
I want upload a file using ajax... So, i want to get the tmp filename with js. Is this possible?

AJAX itself won't let you upload a file via a web form - you need to perform a traditional form post to a window (or a frame/iframe) in order to send binary content. Such a post must be handled by your server code (in Java, PHP, python, etc), which can process the stream into a temporary file (some languages/frameworks such as PHP can do this automatically).
You can then make the temporary file available via a URL scheme, and return this URL back as the result page of the post. If this is all on the same domain as your main page, you can then use Javascript to look "inside" the result iframe to find the temporary URL.

If your using AJAX in combination with your server side language just return the temporary name and pull it in with the AJAX, if can all be done in 1 call.

Related

How can you access the HTTP response from a server using client-side JavaScript?

I'm trying to do client-side processing of some data sent in a server's HTTP response.
Here's what I'm working with: I have a web application that sends commands to a backend simulation engine, which then sends back a bunch of data/results in the response body. I want to be able to access this response using JavaScript (note..not making a new response, but simply accessing the data already sent from the server).
Right now, I am able to do this via a kludgy hack of sorts:
var responseText = "{{response}}";
This is using Django's template system, where I have already pre-formatted the template context variable "response" to contain a pre-formatted string representation of a csv file (i.e., proper unicode separators, etc).
This results in a huge string being transmitted to the page. Right now, this supports my immediate goal of making this data available for download as a csv, but it doesn't really support more sophisticated tasks. Also, I'm not sure if it will scale well when my string is, say, 2 MB as opposed to less than 1 KB.
I'd like to have this response data stored more elegantly, perhaps as part of the DOM or maybe in a cache (?) [not familiar with this].
The ideal way to do this is to not load the csv on document load, either as a javascript variable or as part of the DOM. Why would you want to load a 2MB data every time to the user when his intention may not be to download the csv everytime?
I suggest creating a controller/action for downloading the csv and get it on click of the download button.

Returning JS from PHP

I'm executing script on one site. I'd like to pass some params to php on other site (even other domain) and include JS which should be returned from php request. Is it even possible to do?
You can try using JSON,
Complete processing your script on site 1, convert the variables into JSON or simply make a post request to the site on another domain.
Grab the JSON or post Input from site 1 and execute the the php script on site-2, now pack the output into a JSON,(don't forget to serialize/escape the output using htmlspecialchars) all you have to do is json_encode.
Return the output to the post request from site-1.
All of this will work. But here is the catch, you must not pass scripts over the air unless you know your script is safe. They can be intercepted and can be hacked. And the input also must be from trusted sources.
You might also want to check your CORS settings.

Process cross domain xml

In a web project using jsp, I have following requirement
Upload a file (say an image) to a different domain, which returns an xml that includes details of uploaded file (eg: path of uploaded image etc).
How do I capture this xml to save details in db?
I cannot do a normal html form submit, since I lose control after that. So I tried to implement this by setting form target to an iframe. But I cannot process contents of iframe?
Any suggestions how I can accomplish this?
Can I process this using yql?
Yes, you can do this using YQL. Not easy though, and you'll need to set up your own YQL table.
Each YQL table is a XML file describing how to operate on data. The YQL XML specification allows you to define an <execute> element that you can use to write JavaScript code that does almost anything.
Inside this <execute> block, you can use the y.rest object to POST data somewhere else.
You'll probably have to encode the file using base64 to be able to actually POST data using the YQL syntax, something like INSERT INTO mywebsite.files (name, data) VALUES ('foo.png', 'KBB987987JJBHBGV==') (second param is a illustrative base64-encoded file).
Assuming the server you are submitting the file to is not under your control (so you can't use CORS), you have proxy the request through your own server.
You can't use YQL as you can't make post requests with it.

load file and write in xml or html?

I am curious if this is possible:
on the website:
using jquery to get value from a html tag and store the value as a variable
using ajax to call a file (xml, html or txt) on servside or external site for a php file
define the current value in the xml html or txt file
and add this value to the current value. = new value
store this new value in the xml html or txt (saving the file on server...)
Q1. Is this achievable?
Q2. If so, what file is best used/supported? (php would be my very last alternative option) I prefer xml and html...
Q3. step 1 to 4 i can get to work - but step 5 i have totally no experience in. Anyone can guide me the right direction for writing those documents?
You cannot write the file directly in the server using javascript.
What you can do, for example, is send the updated data to a page in the server (sending a request to a PHP page, or a MVC endpoint, or whatever server-side technology you want to use) and then have the server code update the file using that data.
You can do this with AJAX (eg. using jQuery with $.post) if you want it to happen in the background, or with a normal request (which will reload the page or take the user elsewhere depending on the server code).

How to access a property file using "javascript"

How do I access a property file using javascript. Normally property file is a xml based file.
I java we access a property file like this:
Properties prop = new Properties();
fis = getClass().getResourceAsStream("props.xml");
if (fis != null) {
prop.loadFromXML(fis);
}
String dbUrl = prop.getProperty("dburl");
I want to do the same but using javascript. is there a possible way of doing it?.
JavaScript can't load files, as part of its security model. It can retrieve XML from the server using AJAX, but it can't read files from the client computer.
You can't load any files from the users computer with javascript in the browser.
If the file is from your own server you can load it, like any other ajax, with XMLHttpRequest.
Javascript doesn't use property files, as, either it has all the information it needs in the javascript files or in the html, or it will make an XMLHTTPRequest call to get the information from the server.
The server can look at the property file, and may use information passed in from the request, such as the header information, to know more about the client, to determine what information to pass back.
So, if you want to pass back some localized information, the server would have to get that from the browser request and then it could send back just what is needed for that transaction.
Javascript is different from java, so one limit is that javascript cannot read from the hard drive of the user, and since it is a webpage, the user wouldn't have the property file installed, it would still be on the server.
Javascript can only make requests to the address that that script came from, so there is a second sandbox rule that has to be met.
You may want to better understand javascript, then try to rephrase your question.
HTML5 now allows JavaScript to read local files, via the File API:
http://www.html5rocks.com/en/tutorials/file/dndfiles/

Categories

Resources