I want to redirect on a specific page after file download in MVC. And in my application signalr is already configured. So I used following code in controller
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
hubContext.Clients.All.RedirectToBatch(BatchID, Convert.ToInt32(Session["AgencyUserID"]));
return File(stream, "text/plain", (BatchName + "." + ediExtension));
And in view I used following code
var chat = $.connection.chatHub;
chat.client.redirectToBatch = function (BatchID, AgencyUserID) {
if (currentUserID == AgencyUserID) {
var claimlist = $('#ClaimListGrid');
if (claimlist.length > 0) {
window.location = "/Billing/ClaimSummary/" + BatchID;
}
}
};
$.connection.hub.start().done();
$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 5000); // Restart connection after 5 seconds.
});
I think this code will work for redirect to a page after file download. And this works in chrome proper but In firefox this is not working fine.
In firefox following error is occurring
no element found
AND
Firefox can't establish a connection to the server at ws://localhost:2804/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=4p2zZ%2B%2F0ok4B9uAxcslDlTv4kpT%2BGbW2nav6S%2Fe5RlSqQgS2gaFr7%2BlQiIrHZu5cPapvjoeZNB8bXM6AeF%2B1b1RS0P6y%2FETaNeAC5hDQkGrwH5xgDfjDHWt%2B%2B52K7yaD&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&tid=10.
What should I do for this problem?
Related
I'm trying to make real time application with node.js and socket.io. As I can see the server can see when new user connects but can't return information to client side or something. This is what I've on client side:
<script src="<?= base_url('assets/js/socket.io.js') ?>"></script>
<script>
var socket;
socket = io('http://***.***.***.***:3030', {query: "key=key"});
socket.on('connect', function (data) {
console.log('Client side successfully connected with APP.');
});
socket.on('error', function (err) {
console.log('Error: ' + err);
});
</script>
and this is the server side:
var app = require("express")();
var http = require("http").createServer(app);
var io = require("socket.io")(http);
http.listen(3030, function () {
globals.debug('Server is running on port: 3030', 'success');
});
io.set('authorization', function (handshakeData, accept) {
var domain = handshakeData.headers.referer.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
if ('www.****.com' == domain) {
globals.debug('New user connected', 'warning');
} else {
globals.debug('Bad site authentication data, chat will be disabled.', 'danger');
return accept('Bad site authentication data, chat will be disabled.', false);
}
});
io.use(function (sock, next) {
var handshakeData = sock.request;
var userToken = handshakeData._query.key;
console.log('The user ' + sock.id + ' has connected');
next(null, true);
});
and when someone comes to website I'm expecting to see in console output "New user connected" and I see it: screen shot and the user should see on the browser console output: "Client side successfully connected with APP." but I doesn't show. Also I tried to emit data to user but it doesn't work too. I can't see any errors or something. This is not the first time I'm working with sockets but the first time facing such as problem. Maybe there is any error reporting methods to handle errors or something? Also I can't see output on io.use(....) method
The solution is to pass "OK" sign just after authenticating to do the next method:
io.set('authorization', function (handshakeData, accept) {
var domain = handshakeData.headers.referer.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
if ('www.****.com' == domain) {
globals.debug('New user connected', 'warning');
accept(null, true);
} else {
globals.debug('Bad site authentication data, chat will be disabled.', 'danger');
return accept('Bad site authentication data, chat will be disabled.', false);
}
});
I've looked at multiple solutions to this problem but nothing's working to fix my problem.
I'm using ASP.NET MVC 4.5.
Here are my steps:
Use ajax call in page to upload file.
Within same function that generates ajax call run an ajax call to refresh the page to include the uploaded file, after ajax call is finished.
I'm using this as the first call (to upload) (compliments of another Stack Overflow user):
function uploadFiles() {
document.getElementById('fileupload').onsubmit = function () {
var formdata = new FormData(); //FormData object
var fileInput = document.getElementById('uploadfilenames');
//Iterating through each files selected in fileInput
for (i = 0; i < fileInput.files.length; i++) {
//Appending each file to FormData object
formdata.append(fileInput.files[i].name, fileInput.files[i]);
}
//Creating an XMLHttpRequest and sending
var xhr = new XMLHttpRequest();
xhr.open('POST', '/Dashboard/UploadFiles');
xhr.send(formdata);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
//alert(xhr.responseText);
}
}
return false;
}
reloadMain();
}
The reloadMain() function is:
function reloadMain() {
$.ajax({
url: '/Dashboard/ThumbList/' + currentPath,
type: "GET",
timeout: 5000,
success: function (msg) {
$("#thumb-list").html(msg)
},
error: displayError("Unable to get file listing")
});
}
I have noticed this:
The 'refresh' doesn't include the uploaded file information in the response
IE11 and Chrome act differently.
It seems that the problem is that the controller/system doesn't complete the file operations soon enough (I saw a "denied access...file in use" error when using Chrome.
So, it would seem that the refresh ajax call needs to wait until the file system completes its work.
Would you agree? If so, how can I make this work?
You can either set your XMLHttpRequest async to false:
xhr.open('POST', '/Dashboard/UploadFiles', false);
Or you can call your refresh function in callback:
var xhr = new XMLHttpRequest();
xhr.open('POST', '/Dashboard/UploadFiles');
xhr.send(formdata);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
reloadMain(); //Only refresh after the file post get a 200 response
}
}
I'm building an app for iOS using Phonegap and I've run into some problems. The app runs fine on both a simulator and real device until I close the app using the multi-tasking shutdown (double tap home button sequence..)
Upon reopening the app I find that it becomes unresponsive and you can't interact with it in any way. I've spent a fair amount of time trying to debug this and I've had no joy.
Error wise I have been getting the error Failed to load webpage with error: CDVWebViewDelegate: Navigation started when state=1 appear in the xcode console. After lots of googling it seems that this is caused due to hash tags within the URLs (something that I'm using for scrolling down to links both on the same and different pages). Most of the suggestions recommend updating phonegap/cordova to the latest version. I was previously on 2.8 and I went up to 2.9 and it still didn't work, i'm now on 3 and i'm still getting the same issues.
I've checked the cordova git and updated my CDVWebViewDelegate.m file several times with supposed fixes, nothing seems to work. I had a previous version of the app working on earlier versions of Cordova/Phonegap but I've recently upgraded and i'd rather not downgrade to get it to work..
I should probably note that I'm using zepto for my ajax calls and not JQM, for the hash tag scrolling i'm using the following code (figured this may help given it seems to be a hash issue..)
Hash Change function
// Ajax
var wrap = $('#contentScroller .scroller')
// get href
$('a.ajax').click(function () {
location.hash = $(this).attr('href').match(/(^.*)\./)[1]
return false
})
// load page
function hashChange() {
var page = location.hash.slice(1)
if (page != "" && window.location.hash) {
wrap.hide();
spinner.spin(target);
//setTimeout(function () {
wrap.load('pages/' + page + ".html .page-wrapper")
contentScroller.scrollTo(0,0);
refreshScroll();
//}, 1500);
snapper.close();
$(menuBtn).removeClass('active');
}else{
wrap.hide();
spinner.spin(target);
//setTimeout(function () {
wrap.load('pages/Welcome.html .page-wrapper')
refreshScroll();
//}, 1500);
snapper.close();
$(menuBtn).removeClass('active');
}
}
// check for hash change
if ("onhashchange" in window) {
$(window).on('hashchange', hashChange).trigger('hashchange')
} else { // lame browser
var lastHash = ''
setInterval(function () {
if (lastHash != location.hash)
hashChange()
lastHash = location.hash
contentScroller.scrollTo(0,0);
}, 100)
}
Scrolling
$(document)
.on('ajaxStart', function () {
wrap.hide();
})
.on('ajaxStop', function () {
//wrap.show();
})
.on('ajaxSuccess', function () {
//setTimeout(function () {
spinner.stop();
wrap.fadeIn(700);
refreshScroll();
//}, 1000);
// Local storage scrollTo
var storage = window.localStorage;
var url = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);
$('a.scroll-link').click(function (event) {
event.preventDefault();
url = url.replace('home.html?firstrun#', "");
url = url.replace(url, url+".html");
var myHref = $(this).attr('href');
if (url == myHref) {
var sameScroll = $(this).attr('data-scroll-same-page');
sameScroll = sameScroll.replace(sameScroll, "a#" + sameScroll);
contentScroller.scrollToElement(sameScroll, 1500);
} else {
var diffScroll = $(this).attr("data-scroll-diff-page");
storage.setItem("key", diffScroll);
//Alter value for iScroll
var value = window.localStorage.getItem("key");
value = value.replace(value, "a#" + value);
location.hash = $(this).attr('href').match(/(^.*)\./)[1]
$(window).on('hashchange', hashChange).trigger('hashchange')
// Scroll to element after .5 second
setTimeout(function () {
contentScroller.scrollToElement(value, 1500);
return false;
}, 2000)
// Clear local storage to prevent scrolling on page reload
localStorage.clear();
}
Sample link
<a href="IndexOfTerms.html" class="ajax scroll-link" data-scroll-diff-page="First_year_allowance">
this will then pass the attr "first_year_allowance" through to the IndexOfTerms pages and then scroll down to the element that has that id
Can anybody shed some light on how I might be able to fix this? It's really starting to annoy me so I'd quite like to get a fix pretty sharpish!
Note: Libraries used: iScroll, Zepto, fastclick, snapjs, spinjs
Thanks!
Any idea why:
$('#site-toggle').live('tap', function () {
url = $.mobile.path.parseUrl("http://" + window.location.host.replace('m.','') + window.location.pathname);
window.location.href=url.href;
});
works on chrome on desktop but not on my Android?
On Chrome it gets the page you are now on but on my Android it will always redirect to first page that was loaded.
What about just running a replace on the window.location.href property?
$('#site-toggle').live('tap', function () {
window.location.href = window.location.href.replace('m.','');
});
$('#site-toggle').live('tap', function () {
var fullSiteURL = 'http://' + location.host.replace('m.', '') + $(e.target).attr('data-url');
window.location.href = fullSiteURL;
});
I'm writing an extension for Firefox and it is using page-mod module to run a JavaScript file containing:
function handleServerResponse() {
if (xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
//some code
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
var xmlHttp = new XMLHttpRequest();
var txtname = document.getElementById("txtname");
xmlHttp.open("POST","http://localhost:8080/Jelly/GetStuff",true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send("url=" + document.URL);
i'm keep getting xmlhttp.status==0 and not 200, even if instead of localhost I use the IP address.
Any ideas?
Content script code can't do cross-domain requests - try using the Request module instead:
https://addons.mozilla.org/en-US/developers/docs/sdk/1.1/packages/addon-kit/docs/request.html
Instead of writing the code in a separate script and injecting it into a page using a page-mod, you can implement the request in the main.js script in your add-on.