how to obtain a HttpWebRequest range in javascript? - c#

im doing a project where I should take half of the image from one source and another half from another source and then merge them together.
in c# it works like this:
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("URL");
request1.AddRange(0, 10000);
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("URL2");
request2.AddRange(10000, 20000);
and then I read the streams, merge them into a buffer, and write the buffer into a file.
now I have to create a plugin that does the same thing,
as far as I know that I can create an extension for firefox with javascript.
do you think that is possible to do the same thing in javascript or I should search another method? I dont even know yet how to create a plugin so I dont know if I can use some programming language(maybe I can even use c# or java to directly create a firefox plugin)
can you give me some tips? thanks a lot

Yes you can definately do it with ajax
Here you are
Link
$(function() {
$.ajax({
url: 'range-test.txt',
headers: {Range: "bytes=618-647"},
success: function( data ) { $('#results').html( data ); }
});
});

Related

Using dropzone need to add additional values

I am using dropzone to upload files and images to the DB which works perfect, I generate the dropzone div's and call the dropzone jquery function.
In C# its been received by a WebMethod and files are being uploaded to the database.
Now I need to send several id with however I would like to avoid to implement a ajax call to send these id's. After read the documentation on dropzonejs I could not find a simple solution to do this.
My WebMethod does not accept parameter for now but when I have a good way to implement this on client I can add these to WebMethod.
Did I miss something or Am I only able to do this with ajax?
In short looking for the "data:" object within dropzone as in ajax
I am not sure if I understood your question correctly, the formData is available before each files are sent.
From Dropzone.js. Sending: Called just before each file is sent. Gets the xhr object and the formData objects as second and third parameters, so you can modify them (for example to add a CSRF token) or add additional data.
Sample usage:
sending: function(file, xhr, formData) {
formData.append("test",$('#test').val());
},

How to analyze Json data which is returned by WCF service in HTML using AJAX?

Anyone can help me on how to get the data details from JSON model?
I am using a WCF service which return a JSON type data. It runs well I am sure because I try it from WebClient.
But I want to show the data in my HTML site. I am using the following code, nothing help.
success: function (msg) {
var result = eval("("+msg+")");
$.each(result.UserLoginResult.d,function(i,item){
alert(item.name);
});
It really hurt me, you know.
So I beg your help here, I search from google for hours, No one example can help me. :(.
Thank you all. Finally, I found the problem and fix it.
JQuery already return us Json object not a string, we needn't eval() at all.
Just use msg.d[index][index]!
Happy coding,
Rocky
Did you try the JSON.parse(msg) method?
Then you can simply console.log the answer and find out exactly what to do next.
here is the sample how you do this
$(document).ready(function() {
var jsonp = '[{"Lang":"jQuery","ID":"1"},{"Lang":"C#","ID":"2"}]';
var lang = '';
var obj = $.parseJSON(jsonp);
$.each(obj, function() {
lang += this['Lang'] + "<br/>";
});
$('span').html(lang);
});
​
out put: jQuery C#
or you can use the $.getJSON method:

Jquery Ajax requests not working on IE 10 (due to cache)

I would like to begin with this. I am fed up with IE. I have the code below:
$(function () {
$("#cal").on('click', "#forward", function () {
$.ajax({
url: "Home/Calendar?target=forward",
type: "GET",
success: function (result) {
$("#cal").html(result);
}
});
});
});
$(function () {
$("#cal").on('click', "#backwards", function () {
$.ajax({
url: "Home/Calendar?target=backwards",
type: "GET",
success: function (result) {
$("#cal").html(result);
}
});
});
});
It is an ajax call to a controller action in an C# MVC application. It just goes back and forth a calendar's months replacing the html. Now I know that you need to reattach the event due to the html() call and that is why I use on() with JQuery 1.7. I have used delegate() as well. In FF, Chrome it works as intended. In IE 10 it does not. I am at a loss. I knew that IE had issues with delegate in IE8 and with JQuery < 1.5 but this is not the case.
Does anyone know how to solve this?
I am answering the question just for future reference for other people. It seems that IE is caching AJAX requests for some reason I am unable to comprehend.
I noticed using the (surprisingly good) developer tools IE 10 provides that I was getting a 304 not modified response to my AJAX requests. This was not the case in Firefox or Chrome (200 was the response).
I added the cache: false option to my AXAJ JQuery functions and now it works as intended.
IE never seizes to amaze me.
Brief addition, given what (little) I understand on the subject. Apparently, the XmlHttpRequest spec says that XHR GET commands can behave just like a standard web page retrieval (e.g. clicking on a regular old link), and therefore XHR GET commands can be cached. The IE team has chosen to adhere to this spec, while the other browser makers have not. While I can see some logic in this approach, I think those of us who work with XHR requests every day would emphatically say that we would prefer caching to be off by default, rather than on. (-;
I ran into this a long long long time ago with IE... now I always make it a point now to write my ajax calls with a random trailing key/value pair.
I also add cache: false though I have found by itself it doesn't always do what it should (well, maybe its just IE not doing what it should)
This is how I set them up...
$('#trigger').submit( function(e){
e.preventDefault();
var randnum = Math.floor(Math.random()*1001); //magic starts here
$.ajax({
type: "POST",
url: "folder/file.php",
cache: false,
data: "random=" + randnum, //pure magic
success: function(){
// do stuff here
}
});
});
Got this issue too. It turns out that all of the fixes above will not work if the POST response has cache-control: max-age. The 1st request will fetch the data but after that all requests (even if you add a random attribute) will be 304'd.
In this case IE will not even ask the server if the content changed, it just assumes that if it has a max-age then there's no point in doing a request.
Moreover XHR specs say that 304's shouldn't pass any data so basically you get an empty response for a POST (just on IE 9 and 10).

Check if website is online c# without using HttpWebRequest

Is it possible to determine whether website is online or offline other than using HttpWebRequest command?
Please do not suggest me using Ping method because I want to check website availability.
You should send an ajax call to your header (domain). Add random number after the ? (this is to test even if you're page is cached). This is how it is in javascript:
function hasInternets() {
console.log("hasInternets: " + window.location.href.split("?")[0] + "?" + Math.random());
var s = $.ajax({
type: "HEAD",
url: window.location.href.split("?")[0] + "?" + Math.random(),
async: false
}).status;
console.log("s: " +s);
//thx http://www.louisremi.com/2011/04/22/navigator-online-alternative-serverreachable/
return s >= 200 && s < 300 || s === 304; }
If you're looking for C# codes on this, at least this javascript method will give you an idea.
Is it possible to determine whether website is online or offline other
than using HttpWebRequest command?
No, there isn't other way. You need to send an HTTP request to the site and check the returned status code. In .NET this is usually done with the WebClient class but you could use WebRequest as well if you prefer. And to avoid wasting bandwidth you could use the HEAD verb. This way you are instructing the web server to not send a response body, just a status code that you could check against for being 200.
An option is to load an image, and javascript img with events that get fired
onabort Loading of an image is interrupted
onerror An error occurs when loading an image
onload An image is finished loading
for more info
http://www.w3schools.com/jsref/dom_obj_image.asp

jQuery delivery of a file with AJAX call

If I have an AJAX call that returns, say, a CSV, how do I get the browser to prompt the user for a download? Below, the ProductsExport will return the CSV in the success data. I just need what I'd replace the // Deliver file to user line with...
$.ajax({
type: "POST",
url: "/Search/ProductsExport",
data: $('#CustomerId').serialize(),
success: function (data) {
// Deliver file to user!!
},
error: function (xhr, textstatus, errorThrown) {
alert('Error');
}
})
My C# code on the back end looks like so:
var aFileContent = Encoding.ASCII.GetBytes(export);
var aMemoryStream = new MemoryStream(aFileContent);
return File(aMemoryStream, "text/plain",
string.Format("{0}.csv", CustomerId));
You cannot as far as I'm aware. You can't use ajax here as a file download.
YEs - its a support datatype as per jQuery but not for a file. You need to link to the file for a non ajax request either via a link or a jQuery get request.
See:
Unable to open download save dialog
and
"datatype" on
http://api.jquery.com/jQuery.ajax/
Alternative solution to your problem is have an AJAX function return an actual URL that will download csv (similar to your C# backendcode). The client side will then launch the URL using window.open(url)
Why does it have to be ajax? Just build your url and do a window.location.href to execute your call. All you seem to be passing it is a customerId, so that should be pretty easy.
Ajax operations are meant to allow a user to stay on the page while operations continue behind the scenes. A file download will keep the user on the page and just download the file, so there's no benefit to using ajax in this siutation. Something like this perhaps:
window.location.href = "/Search/ProductsExport?" + $.param($('CustomerId'))

Categories

Resources