how to read text file from django server using ajax? - ajax

I wanna know how to read text file from django server using ajax? or something else.
I searched about using XMLHttpRequest and It worked when I tryed to get django template html file. but I couldn't get other text file outside of templates directory. how can I send content of requested file in veiw.py ??
I think I cant use render_to_response()..
I need your help:(

Simething like this in your url:
url(r'^static/mymedia/' , 'myproject.views.get_file'),
Something like this in your views:
def get_file(request):
current_directory='/home/arpit/myproject/'
f=open(current_directory+request.path)
return HttpResponse(f.read(),mimetype='text/plain')

Related

How do I write a POST variable to a text file within SharePoint 2010

I’m working on a project that uses SharePoint 2010. I need to write a POST variable to a file using an ajax call.
If I were using PHP I would use the fwrite() function to write the POST to a file.
Here is how I envision my solution working. When you go to notarealwebsite.com and submit the form, I envision using an ajax call to write the file. The ajax on the index.php would look like:
$.ajax({
type: 'POST',
url: 'save-text.php',
data: {json: JSON.stringify(strJson)}
});
In PHP I would pass the POST variable into the save-text.php file and its code would look like this:
<? php
$file = fopen("file.txt","w");
fwrite($file, $_POST['json']);
fclose($file);
?>
Does SharePoint have an equivalent function I can use to save the POST to a file?
You don't give us too much here...
You can develop something in JavaScript using a third library like SharepointPlus with the createFile function
You'll do:
$SP().createFile({
content: JSON.stringify(strJson),
destination: "http://mysite/Shared Documents/file.txt",
url:"http://mysite/",
after:function() {
alert("File saved");
}
});
The createFile of SharepointPlus uses the CopyIntoItems web service of Sharepoint.
In SharePoint environment, you would most probably write these strings to list instead of file system. This is especially true if its some kind of configuration value because you want it accessible from all the frond end machines. A file on a filesystem will be created on just a single node where the code runs ultimately after the load balancer.
See:
How to: Create, Update, and Delete List Items
http://msdn.microsoft.com/en-us/library/office/ee539976(v=office.14).aspx

Ruby - open-uri doesn't download a file itself, but just the HTML code of the website

I am trying to use this snippet:
open("data.csv", "wb") do |file|
file << open("https://website.com/data.php", http_basic_authentication: ["username", "password"]).read
end
But instead of the desired CSV file, I get just downloaded the HTML code of the website. What's the problem?
When I access the URL and I am not logged in, then it's displayed the form for login (not the HTTP authentication window).
How to solve this situation?
Thanks
I think you should try out net/http http://dzone.com/snippets/how-download-files-ruby
It's probably because your php script return a response with a mime-type different of text/plain or better : text/csv
please see this related previous response
How to use the CSV MIME-type?
in the PHP Script :
header('Content-Type: text/csv');
header('Content-disposition: attachment;filename=data.csv');

Loading a JSON file in Firefox Addon Page Script, everything locally within the package

I have been developing a Firefox extension using the Addon SDK and need to load some data that is stored in the same package, as a separate file: "data.json". It needs to be loaded from a page script, i.e. "loader.js" which is included in the "panel.html" using the script src tags.
The structure is like this:
+data
panel.html
panel.js
loader.js
data.json
...
+lib
main.js
...
panel.html has:
<script type="text/javascript" src="loader.js"></script>
Initially we stored the data simply into a js file as "data.js" and included from the "panel.html" using script src tags and it worked without any problems. However when we submitted the add-on to the Mozilla Addon site, this was addressed as one of the issues to fix, saying that we need to use a non-executable format, such as a JSON file to make it more safe.
Now the problem seems like "loader.js" is not allowed to make a AJAX request to "data.json". (Using the JQuery $.ajax() call returns with no success, giving the error code 0) So the solution I have been thinking of is to load "data.json" from "main.js" using the SDK's request() function and somehow pass it to the "loader.js", the page script. But that seems to be complicated since, as far as I understand, the data needs to be first sent to a content script, and then from there to the page script. And this needs to be happen when the page script is loading! I am confused about this since I am not sure if I am missing a much more practical solution, or is it really something complicated what I am trying to do, simply loading local JSON data in the package into a local page script?
Here's an example on the Add-on Builder that explores and approach to this.
First off, you can load the json file from data and parse it using self.data.load:
let data = require('self').data;
let taps_data = JSON.parse(data.load('taps.json'));
This loads synchronously, so it isn't something you want to do often, in the example it would only happen when the add-on firsst becomes active in a browsing session.
Next, you would use content scripts and message passing to pass the data in to the panel.
In the main.js script:
panel.on('show', function() {
if (!loaded)
panel.port.emit('taps-data', taps_data);
});
In the content script:
self.port.on('taps-data', function(data) {
$('#list').html('');
data.forEach(function(item) {
$('#list').append('<div>'+ item.name +'</div>');
});
self.port.emit('taps-loaded');
});
I do a bit of extra work to make sure I'm only emitting the data once. The data, FYI, is saved from the live beer keg data api from my local pub.

How to load the data using Ajax in Django template?

Using Ajax in Django is a open Issue. I have tried to understand it by reading blogs and forums, but it didn't work for me. I am posting a very simple question related to it.
Method defined in views.py: (just a sample)
def widget_data(request):
####
extra_context = {
'data': username
'part': company
}
return direct_to_template(request,'test/widgets.html',
extra_context)
I want to load extra_context to rendered template using Ajax.
Following things will happen in widget.html template i.e.
When a moderator will type a URL at the address bar to open a page it will load two widgets i.e. one for loading all the registered username and other one for their company name . user are continuously registering to the sites and adding company name to their profile. When a new user will registered to the site both widget should load automatically using Ajax.
I have no idea about the topic of Ajax.
How to do this?
How should i even start this?
I have read these following links :
Tutorial 1
Tutorial 2
I know that the answer will be too long and too messy but any help will be appreciative.
If you use jQuery you can do the following
$.get('/url/of/widget_data/view', success(data, textStatus, jqXHR) {
$('#idofdivtoupdate').html(data);
});
That would probably be the easiest way to do it.

POST request in Windows phone

I am trying to get some data from the user and send it to my server for logging purposes. I was able to do it easily with jquery for my website. When I try to write a windows app for the same I am clueless as to how to post the data to my server.
So far I have fetched the data from the user and I am trying to use OpenWriteAsync function of webclient to send it to my server. I understand how OpenWriteAsync function works but I am not able to figure out how to send my own data.
My php script in my server will get values with name "FILENAME" and "FTIME" like,
$fname = $_POST['FILENAME'];
$time = $_POST['FTIME'];
So I have got this information from the user in two different string variables in c# inside my app. Now how do I send this to my server so that my php script gets this value and logs it inside my server.
I hope I am clear.
Take a look at UploadData method.
The way you use it something like this:
UploadData("http://abc.com","POST", "data in byte array");
Note: you need to convert data to byte array
Reference: http://msdn.microsoft.com/en-us/library/ktfa4fek(v=VS.90).aspx
You can do this by using the ContentType property in the HTTPWebRequest object.

Resources