jQuery File Upload – UI version submit multiple files with single post request - jquery-file-upload

Even if you set singleFileUploads to false, if you do multiple individual selections, still sends with individual requests as below.
$('#fileupload').fileupload({
singleFileUploads: false
});
Way forward?

Related

GWT panel fields data refresh delay on slow internet

I am using GWT 2.5.1.
In my GWT web app I have a ComplexPanel object which contains a set of fields(widgets). There are a suggested field (on panel) which gives me the opportunity to find object and info about it. Fields (10-15 of them) contain info about that object.
The problem is when user (client side) has a slow internet connection, fields on form are updated with a delay. And if in moment of delay user click 'Save' button (AsyncCallback), old data (which is not updated) posted to the server.
How it works:
1. Server receive callback from the form and start to processing the data.
2. Server refresh all the fields with new data and end the works.
3. Javascript updating the data on form using about 10 requests.
But: internet is slow and one part of data is refreshed and other no.
4. User click SAVE and mixed data goes to the server.
I need to know (from server side) when all the field are refreshed on client side and server can proceed with a next post request.
Thanks in any advice.
Perform a validation check on click of the save button. There are several things you can do as per your requirement. I have listed one way to validate it.
Set a flag before making the async call as false. For instance, isLoaded = false
Once you have all the fields just update it to true, i.e. isLoaded = true
On save button handler check for isLoaded flag. If false, prompt a message, else save.
Update:
You can count the number of response received. You know that you will get 10 response. So for every response received increment a counter. Activate save only if you have all 10 response recieved.
For a clean way to do this, use gwt-async-future.

Downloading a PDF from an HTTP POST call

Here is what I'm trying to accomplish (IE 9+, Chrome, FF, Safari) without the use of JQuery:
Make an http POST call to my API endpoint with some data
Server dynamically generates a PDF and returns the PDF as a binary attachment
Browser does default download behavior and downloads the PDF without refreshing the page
Basically I want to get the behavior similar to <a href="test.pdf"> but for a dynamically generated PDF after making a POST call instead of a GET call.
I've tried lots of different things, but they either didn't work cross browser (such as using $window.open() with a blob URL), were blocked by popup blockers (any $window call outside of the click scope), or didn't cause the PDF to be automatically downloaded (any $http POST solution).
I finally found one solution that seems to work which creates a form using javascript and submits it.
var form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', myurl);
var params = {foo: 'bar'};
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', key);
hiddenField.setAttribute('value', params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
This successfully accomplishes the 3 steps above, but now I've run into a new problem. There is no way to determine when the PDF file has been successfully downloaded. This is preventing me from removing the form and from displaying a friendly 'Please wait...' message to the user. There is also the additional problem that submitting the form cancels any outstanding ajax requests as well which isn't optimal.
I have full control over both the server and the client, so what's the best way to fix this? I don't want to have to save the PDF on the server so passing back a url and doing a second GET request from the client won't work in this case. Thanks!
You can make an server response behave as a download by applying some HTTP headers:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="SOME_NAME.pdf"
Content-Transfer-Encoding: binary
If you're initiating the download through JS only (instead of having the user click a download link), then check out this question for some caveats.
Update: Syntax for POST
Better Update: Form solution with iframe target
You can detect that your server-side script has finished (and subsequently, that the download is ready to begin) by having the form target an iframe. I believe this should also fix the issue of cancelling outstanding Ajax calls, but I'm not certain. Here is the code to do it (just stick this into your code example after the for loop and before document.body.appendChild(form);):
var frame = document.createElement('iframe');
frame.setAttribute('id', 'pdfFrame');
frame.onload = function(){
document.body.removeChild(form);
document.body.removeChild(frame);
alert('Download ready!');
}
document.body.appendChild(frame);
form.setAttribute('target', 'pdfFrame');
You can replace my alert with your code to remove the 'Please wait...'.

How to get url of a tab before the page gets loaded in Mozilla Addon SDK?

I am creating an addon for blocking sites on user request.I have done- getting user input and storing in simple-storage.Now i want to access the url of the tab(s) before the page gets loaded so that i can process the url and gets it hostname to block the site.
You do this using the PageMod module with onAttach.
pageMod.PageMod({
contentScriptWhen: 'start', //This says not to wait until the page is ready
include: ['*'],
//Forget about contentScript(File), we're not attaching a script
onAttach: function(worker) {
var tabUrl = worker.tab.url;
if(tabUrl==myString) worker.tab.url = 'http://arabcrunch.com/wp-content/uploads/2013/05/block-website.jpeg'
}
});
But I recommend doing it differently. Instead of checking every URL yourself then doing something , you create an array of URLs or partial URLs and set include: myArrayOfUrls. Then you don't need the if clause in onAttach, you already know that it's one of the websites you want to block.
You can register to the http-on-modify-request notification from the Observer Service. You'll get notified just before the request is made and you can get the URL.
See the following resources on the topic:
Setting HTTP request headers for registration example and Observer Notifications for the list of notifications you can register to.
What you describe is essentially what the Adblock Plus extension is doing, maybe you could use it directly or look into its code.

Simplest example for sending post data via links in Zend Framework

Starting with Zend and I´d like to know what is the simplest way of sending POST data to another page, not by forms, but by some link in my view instead. Thanks :)
You can't send POST data through a link. At least not through a normal link. Link can only carry GET data.
If you need to send POST over a link it's most certainly a design flaw.
If you're 100% sure, that you need it, you can do that using jQuery and onclick event. It`s not possible to do it without javascript. Other option would be to send it using form with hidden fields with single submit button visible - that would even work without javascript.
Normal hyperlinks in HTML are sent with GET requests and are not supposed to change the state of the resource being accessed. This is known as being idempotent. You can repeat the request over and over, and the result of each succeeding request to the same URL is the same as the first one.
POST requests don't have this restriction and are intended for when the user needs to change something (such as creating a new resource.)
It's not possible to send a POST request via a normal HTML link. And even if you find a way, it breaks an almost universal expectation that web users have. What are you trying to accomplish? Maybe there's a better way.
But to answer your question, you could use something like jQuery to capture the "click" event and make it do a POST request:
$('.my-link').click(function() {
var url = $(this).attr('href');
var data = {};
$.post(url, data, function() {
window.alert('success!');
});
return false;
});
If your URL has any query parameters, i.e. "?foo=bar&baz=bum", then you'd probably need to strip them off of the URL and pass them as a second parameter to the $.post() function. This is left as an exercise for the reader. ;-)

Post/Redirect/Get pattern for HTTP Responses with application/excel MIME Type

I want to post some data to the server, and in response, I want to create a CSV file, with application/excel as the MIME Type (recently recognized as Internet Media Type), to force the browser to open the generated CSV file in Microsoft Excel. However, I also want to prevent user from re-submitting the same info (re-posting the form) by any accident as the result of refreshing the page.
With simple CRUD operations, I use Post/Redirect/Get pattern, so that any further refreshing will only send HTTP Get Request to the server, without any parameter, thus not changing server's state (Idempotence).
What is the recognized pattern for stopping user from re-submitting (re-posting) the same info to the server, when the response is not a page, but a file?
Any idea?
The Post/Redirect/Get pattern is an answer to a browsing event.
Here, there is no browsing action (the form submission only open a 3rd party app, i.e excel), and so any kind of browsing related pattern will be useless.
I suggest you use both a server side trace of the initial submission (with a unique token maybe), so you can prevent the file generation, and an easy to write client side script like <form onsubmit="this.onsubmit = function(){ return false ; }">
I can offer you one other solution.
Take hash (MD5/SHA256 ..) of your submitted data. The hash will be [fairly] unique.
Put it in list in a session with a time limit, say 5 minutes.
Even your user submit same data. Hash will be same and you can give error message to your user.
If different users can post same data, you can also hold user information in the list. And give error message according to user.