ExtJs3. Defect when the upload file - image

To upload a file using inputType: 'file', and for a form prescribed fileUpload: true.
Everything works fine, the file is saved. But after saving the file block success (from ajax request) is not satisfied. Ie all the stops to waitMsg: 'Saving Data ...'.
What could be wrong?

Also, remember to set the Content-Type header to correct MIME type in your server response: "text/html". Anything else will result in ExtJS throwing an error when decoding your response.
In PHP, this can be done with
<?php
header('Content-type: text/html');
echo json_encode(array('success' => true));
?>
From ExtJS API docs:
If the server is using JSON to send the return object, then the Content-Type header must be > set to "text/html" in order to tell the browser to insert the text unchanged into the document body.
Characters which are significant to an HTML parser must be sent as HTML entities, so encode > "<" as "<", "&" as "&" etc.
Make sure you do escape special HTML characters as suggested. If you don't, ExtJS may still succeed in parsing the server response but with unexpected twists: single quotes in HTML-like strings turn into "', etc.

Maybe you should send back server result to extjs form by print this on server:
echo '{success:true, data: "save" }'

Related

Using Perl, How do I save an image from an external script (PHP) URL

I have a Perl script that needs to retrieve an image from an external URL and save it locally. In some cases, the URL is a direct URL to a PNG file and my code works perfectly to capture the image data and save it locally.
My Perl code looks like this:
$ua = LWP::UserAgent->new();
$ua->agent("MyAgent");
$response = $ua->get($iconpath);
$content = $response->decoded_content(charset => 'none');
open(ICONFILE, ">$localimagefile");
binmode(ICONFILE);
print ICONFILE $content;
close(ICONFILE);
But in some cases, the URL for the image on the external site (iconpath) is a PHP script, like this:
http://forecast.weather.gov/DualImage.php?i=sct&j=hi_tsra&jp=30
In this case, the local image file is created, but it's zero bytes. When I try to dump $content after the request, it's empty and it's length is zero. For a direct URL to a PNG file, this same code works perfectly and saves a valid copy of the image file on the server.
I've tried various methods using wget, curl, backticks, and LWP:Simple (getstore), but I always have the same empty file results with the images generated externally using the PHP URL.
Any ideas how to do this would be greatly appreciated.
The problem was that the actual data returned from the weather API had the "&" character replaced by the HTML entity code & amp ; (ignore spaces -- added so it shows up here). I didn't notice this when I dumped the returned URL to the browser since it simply displayed as a regular ampersand (&). The simple solution was to replace the HTML entities with actual ampersand characters before calling the ->get with that URL, which works perfectly and returns the image data or saves it directly when using ":content_file => $filename" in the ->get call.

Unable to get HTML tags in RequestParam (MVC)

I am trying to send a get request through an MVC form but when I get the value in the controller using RequestParam somehow the HTML tags like <li> etc are getting filtered, any suggestion so as where these tags are getting filtered and any workarounds for them.
Thanks.
You can try using some URL Encoding like this %3Cli%3E%3C%2Fli%3E . If this is not working, you can try a hardcode encoding when sending and decoding when receiving. For example replace "<" with "|OPEN_BRACKET|" and ">" with "|CLOSE_BRACKET|" . Keep in mind, that this is a bad solution and use it as a last resort. Also make some checks for XSS attacks.

ExtJS file upload ajax response strips HTML from inside string inside JSON

I have a form with a fileupload control in it, and I call form.submit with a success function.
My server side does the usual trick of setting the content type to text/html to get it to arrive in one piece.
In the success function, action.response.responseText does contain the JSON which I sent.
When it leaves the server, it looks like:
{
html: "<div>a div</div>"
}
When it arrives in the success function, the tags are missing. What's going on? Do I need to put some sort of html cdata wrapper around the entire response on the server to avoid this?
A string in a string in JSON. As long as it is well-formed you are allowed to put HTML in string values (making sure you escape quotes etc.).
It's probably the function you're using to insert the HTML that's stripping the tags.
Here's the situation. When you ask ExtJS or JQuery to do Ajax for a form with a file upload, it has to use an iframe. For the response to come back correctly, it has to be of content type text/html whatever is in it. So it has to have it's HTML characters escaped for HTML, which I accomplished with a function from CommonsLang.

retain "+" sign in string

I am creating a base64 string. There is also '+' sign in that string. I am sending it through ajax to the database. but ajax removes the + and replace it with white pace. How I can overcome this problem
How are you sending it to your server ?
You must urlEncode what's in the url, or use JSON.stringify for an object you send as json.
This means you must decode it on the server side but most server technologies handle this for you.
This is because you are not url encoding the base64 before sending it.
Run your base64 through javascript's escape() function before sending it.

How do I receive and respond to Ajax requests in Perl?

I have a client-side appliction which submits some data via AJAX POST request (request.open("POST", url, flag)) to my Perl CGI script.
How do I retrieve this data with Perl and return some other data (AJAX response)?
The same way you would handle any POST request. You need a CGI script at url that receives the POST and returns whatever the JavaScript is expecting. The only difference from a normal HTML form POST is the Content-Type that you would be receiving and transmitting.
Use the core CGI module. E.g.
use strict;
use warnings;
use CGI;
my $q = CGI->new;
my $foo = $q->param( 'foo' );
print $q->header;
print "You said $foo";
If your app will be large and complex, you may want to investigate one of the Perl web application frameworks, like CGI::Application or Catalyst.
The basic model for CGI scripts is "read from STDIN, write to STDOUT". At the input stage, the environment variable CONTENT_LENGTH gives the length in bytes of what is to be read from STDIN. At the output stage, you also need to send basic HTTP headers, which minimally is one line "Content-Type" with a mime type, like text/html, or text/plain, etc. plus a blank line:
Content-Type: text/plain
<your data starts here>
In the case of XMLHttpRequest, you completely control the format of the data, so how you parse the input from STDIN is up to you. Similarly you can lie in the mime type, and send whatever you want as a response.
JSON is a nice format for sending data from Perl to JavaScript.
use CGI;
my $q = CGI->new;
my $xml = $q->param('POSTDATA');

Resources