Get image using JSONP C# - c#

I want to get an image by passing a url using JSONP, what is the best way to go about this, can I do this without a library. What library should I use? any tutorials? thanks
Use a Callback Specify a callback URL via the callback=http://??? GET parameter. The URL will be called via a JSONP request and the location of the image passed, the receiving end can then integrate the image into the app. (the callback parameter should be urlencoded)

for cross domain request I suggest this link:
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
try to pass the image path, it might work or with jsonp I think the other site should have web method that transforms the image into bytes and return it. You can search about Ajax upload download image to see how things can work for image.

Related

C#: How to GET an image from an API with credentials?

I'm trying to make an API call using the simplest way possible:
<img src="http://IP-ADDRESS-TO-API/api/call/camera=1">
<img src="http://IP-ADDRESS-TO-API/api/call/camera=2">
IN CHROME: (the above code will pull the first image, but not the second almost every time)
IN EXPLORER: (the above code will only pull the first image AFTER prompting the user for the username and password)
The 'IP-ADDRESS-TO-API' is actually not the same IP or domain as where I'm calling it from. If I call the address from an AJAX call, I get a No 'Access-Control-Allow-Origin' header error.
I'm attempting to call this via C# to see if it can bypass this with the proper header sent in. Is there anyway to call this API call while passing in a username and password?
I've looked at examples of doing this via WebRequest but sadly, I can't see any methods of passing in credentials via the call itself. This as well as actually returning a proper image back to the DOM. Can anyone help?
Have you looked at the Credentials property of the System.Net.HttpWebRequest object?

How can I download only part of a page?

I have 100 pages on my site, but I want download only part a page instead of all page content.
I want just one box of each page to download, the file size is 10 KB.
For this I Use WebClient and htmlagilitypack .
WebClient Client = new WebClient();
var result = Encoding.GetEncoding("UTF-8").GetString(Client.DownloadData(URL));
Unfortunately, that's not possible, because HTTP is not designed to deliver a specific part of a web page. It does support range requests, but for that you would need to know where exactly (in terms of bytes) the desired content is located.
You can
download the whole page and then
use a HTML parsing library to extract the part you need.
You cannot achieve this.
The only solution is changing the website structure itself. if you have control of the server -
Change the architecture of your website, making the data in the box accessible via an ajax call.
Now you can get the data via the WebClient.
If that data is already served via a API call, you can point your WebClient to that URI Instead.
Here is an example of structuring you website based on ajax -
AJAX with jQuery and ASP.NET

How to download pdf with no url

I am building a system which sells pdf's to our customers. We have a 3rd party which creates and manages the pdf's. When a purchase is made, our api calls their api which returns a byte[].
How do I start a download from this?
Example process:
User clicks Purchase
Javascript makes ajax call -> /api/PurchaseProduct/product/information
C# then calls 3rd party api in similar fashion
Return is a byte[] - Which is the document
From here I don't know if the byte[] should be returned to the browser or if something else should happen. If I return the byte[] does javascript then need to turn it into a pdf? Should C# be turning the byte[] into a pdf and then return that?
I am afraid this is very new and im not sure where to start.
Thanks
Edit
We use ASP.Net MVC and our site is a single page application.
File size varies depending on what is purchased.
There are no way of returning a file to the user via AJAX. The browser has to request the file using a normal, HTTP request.
The best solution would be to save the file to disk after the ajax request, and then return the path to the client so that the file can be opened as usual with e.g. window.open.
In the method, which is called by AJAX, include the following:
Current.Response.ClearHeaders();
Current.Response.ClearContent();
Current.Response.AppendHeader("Content-Disposition", "attachment; filename=file.pdf");
Current.Response.ContentType = "application/pdf";
Current.Response.BinaryWrite(returnedByte);
Current.Response.End();
where returnedByte is the byte[]

How to use web service methods in ssrs?

I am new to the SSRS i have created a web service which has one method which takes the string value and returns image of bar-code of that value method is as below
Image BarcodeText(string textToBarcode,int barcodeWeight,bool isMargin)
i want to call this method in my SSRS report from the web service in the image box expression and pass the value to the method i don't know how to call a method from the web service. I searched for accessing method from the web service but came up as no help so any links or any description would really helpful for,
1) How to reference web service in the report ?
2) How to call method from web service and pass the parameters to the method ?
Thanks in advance.
I have got same problem some time back so i got a work around for this problem i hope this will work for you too.
1) First i created the web app with the service reference of the created web service suppose Barcode39 is service so add the reference to the web application.
2) Then write the code behind using service client and call the method to create the barcode image in that code by passing the request variables values
3) Now give the link of that page as the URL to the image expression along with setting the image type as "EXTERNAL" and pass the values to the URL as request variables as
"http:\\localhost:2310\BarcodeImage.aspx?data=" +(Parameters!YourParamtere.Value)
And one thing instead of returning the Image from the service try returning the byte array using stream and .ToArray() method that would be the best practice.Let me know if any problem.
http://technet.microsoft.com/en-us/library/aa964129.aspx
This webpage give you all the info you need for accessing web service from SSRS.

How to programatically upload/post an image to an image hosting website without a browser?

I want to write a simple utility to upload images to various free image hosting websites like TinyPic or Imageshack via a right-click context menu for the file.
How can I do this using .NET? I've seen some linux scripts that use cURL to post images to these website but I'm not sure how I could create the post request, complete with an image in C#?
Can someone point me in the right direction?
EDIT:
I've found a pretty good resource. Cropper, a free screenshot tool written in .net, has a lot of open-source plugins. One of them is a SendToTinyPic.. complete with source. Link here:
http://www.codeplex.com/cropperplugins
The FlickrNet API makes this extremely easy for working with Flickr from .NET. You have to have a Flickr account as well as an API key and shared secret. Once you have what you need, working with the API is very simple:
// http://www.flickr.com/services/api/misc.api_keys.html
string flickrApiKey = "<api key>";
string flickrApiSharedSecret = "<shared secret>";
string flickrAuthenticationToken = "<authentication token>";
Flickr flickr = new Flickr( flickrApiKey, flickrApiSharedSecret );
flickr.AuthToken = flickrAuthenticationToken;
foreach ( FileInfo image in new FileInfo[] {
new FileInfo( #"C:\image1.jpg" ),
new FileInfo( #"C:\image2.jpg" ) } )
{
string photoId = flickr.UploadPicture(
image.FullName, image.Name, image.Name, "tag1, tag2" );
}
Use HttpWebRequest.
Using this class, you can POST data to a remote HTTP address, just set the mime/type to multi-part/form encoded, and post the binary data from the image with the request.
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(VS.71).aspx
For ImageShack, take a look to this Application.
TinyPic.com doesn't have an API as far as I know, but the Cropper SendToTinyPic Plugin tries to upload using "Screen scraping". The official version of the plugin doesn't work right now, but I put together a patch using the same approach, and submitted it to the cropperplugins project. It's just one source module that changed. Anyone can download the plugins project, and then drop in my patch and it should work.
With the patch, it's PritScrn or Alt-PrntScrn will save the image and upload to tinypic, and stuff the URL of the raw image on your clipboard. All in 2 seconds. easy.
If you don't want the actual tool, you can still look at the source code of my patch to see how to POST a page with form-data and a file upload. No direct link. See http://cropperplugins.codeplex.com/SourceControl/PatchList.aspx and look for #3239.
This example image was produced and then auto-uploaded to tinypic.com with the Alt-PrtScrn key-combo.
To embed it here, I just had to ctrl-V because the URL is stored on the clipboard.

Categories