I have been provided with an ashx "service" that returns an image. I'm new to ashx files - so don't know how to handle it.
I need to stream the image into a byte[] so I can copy it somewhere else. How do I do that?
You can use WebClient.DownloadData pointing to asxh.
So let me give you a sample. Let's say you have image located on asp.net page, like this:
<img src="http://someServer/someSite/MyHandler.ashx?id=myId"/>
In this case you can use following code:
using (System.Net.WebClient wclient = new System.Net.WebClient())
{
byte[] data = wclient.DownloadData(
"http://someServer/someSite/MyHandler.ashx?id=myId");
}
Alternatively you can use WebRequest
Related
i want to upload an image to php server. i dont know how to do it. i tried with below codes
System.Net.WebClient Client = new System.Net.WebClient ();
Client.Headers.Add("Content-Type","binary/octet-stream
byte[] result = Client.UploadFile ("http://your_server/upload.php","POST","C:\test.jpg
String s = System.Text.Encoding .UTF8 .GetString (result,0,result.Length );
but image shouldn't reach php. i want to use multipart/form data method. can anyone guide me?
I'm so stuck on something i thought would be easy.
I have a DLL that returns an Image object.
I just cant figure out how to display that image on a webpage.
I've tried a few ways, and google a million different variations.
Is it not possible to just bind an Image object to an element on the page like an HtmlImage or a simple img?
Or do i need to convert the Image to a Stream? or a Bitmap? I'm really stuck!
Any help appreciated.....
V
With Asp.Net WebForm, the easiest way is to create a custom ashx file.
In Visual Studio, create a new Custom Handler (I'm not sure of the name of the template in Visual Studio). This will create a .ashx file.
In the code of this handler, write something like (does not have VS under the hand to test the syntax) :
public void ProcessRequest(System.Web.HttpContext context)
{
byte[] raw;
using(var ms = new MemoryStream()){
Image myImage = GetFromDll();
myImage.Save(ms, ImageFormat.Png);
raw=ms.ToArray();
}
context.Response.ContentType = "image/png";
context.Response.BinaryWrite(raw);
}
Then, in your browser, navigate to http://yourserver/app/yourhandler.ashx.
You can if you want add url parameter, and get it from the Request.QueryString collection
It's not as simple as binding. On the client side images are retrieved from the web server as a separate GET request, which means you have to have a URL that resolves to an image. The other option, as Asif suggested, is embedding your image in the HTML as a Base64 string, which is bad practice for shared images (see Steve B's comment).
You either have to provide an URL (route that returns the image file in MVC, or a custom page with proper content type and Response.Write in WebForms), or embed in html.
EDIT:
There is also a third option involving custom HTTP handlers. These have the advantage of bypassing the app framework and serving the content almost directly off the web server, see MSDN.
Convert your image to base64 string and then set it in the <img/> tag.
<img/> can show the image in base64 string.
Alternatively you can save the image and use the path in the <img/>.
I am using WebClient.DownloadFile to download a small executable file from the internet. This method is working very well. However, I would now like to download this executable file into a byte array rather than onto my hard drive. I did some reading and came across the WebClient.DownloadData method. The problem that I am having with the downloadData method is that rather than downloading my file, my code is downloading the HTML data behind my file's download page.
I have tried using dozens of sites - each brings me the same issue. Below is the code I am using.
// Create a new instance of the System.Net 'WebClient'
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
Uri uri = new Uri("http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe");
byte[] dbytes = client.DownloadData(uri);
MessageBox.Show(dbytes.Length.ToString()); // Not the size of my file
Keep in mind that I am attempting to download the data of an executable file into a byte array.
Thank you for any help,
Evan
You are attempting to download a file using an expired token url. See below:
URL: http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe`
Server: www35
Token:
4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B
You can't just download a file by waiting for the timer to end, and copy the direct link, it's a "token" link. It will only work for a specified period of time before redirecting you back to the download page (which is why you are getting HTML instead of binary data).
Workaround
You will have to download the multiupload's HTML and parse the direct download link from the HTML source code. Only this way provides a sure-fire way of getting an up-to-date token url.
How #Dark Slipstream said, you're attempting to download a file using an expired token url
look how get the new url:
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
Uri uri = new Uri("http://www.multiupload.com/39QMACX7XS");
byte[] dbytes = client.DownloadData(uri);
string responseStr = System.Text.Encoding.ASCII.GetString(dbytes);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(responseStr);
string urlToDownload = doc.DocumentNode.SelectNodes("//a[contains(#href,'files/')]")[0].Attributes["href"].Value;
byte[] data = client.DownloadData(uri);
length = data.Length;
I dont parsing the exceptions
for showing full size images on my site I've decided to use Jquery.colorbox , this pluging works well with static image location like :
<a rel="ex1" href="http://www.blah.com/image.jpg"><img src="http://www.blah.com/image_thumb.jpg"/></a>
but when I want to get the images from a directiry using binary read/write this plugin showing me garbage data not a compiled jpg/image like following :
<a rel="ex1" href="http://www.blah.com/getimage.aspx?id=1234"><img src="http://www.blah.com/getimage.aspx?id=1234"/></a>
and here is my snippet code for getting dynamic image :
thumbLocation = DataHelper.GetItemPicture(recordID);
using (FileStream IMG = new FileStream(thumbLocation, FileMode.Open))
{
//FileStream IMG = new FileStream(thumbLocation, FileMode.Open);
byte[] buffer = new byte[IMG.Length];
IMG.Read(buffer, 0, (int)IMG.Length);
Response.Clear();
Response.ContentType = "image/JPEG";
Response.AddHeader("Content-Length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.End();}
how can I fix the problem?
Use colorbox's photo property. Example:
$('a.example').colorbox({photo:true});
The reason is that colorbox's regex to auto-detect image URLs is going to fail for that kind of URL (doesn't contain an image-type extension).
Some ideas
Change the content type to "image/jpeg" (The caps might matter)
Add the following to the end of the url &thisisan.jpg (Some browsers will not create an image if they don't see this at the end of the url)
Test by putting the image url directly into the browser.
I have a html to pdf conversion tool which resides on our server that I can access using a url with querystrings.
Something like myurl.com/createpdf.aspx?page=http://www.somesite.com.
The tool then converts the web page and displays it as a pdf.
I would like to offer this functionality as a web service, so that our clients can access this programmatically.
Ideal case would be, that our clients send in the site they would like to have converted and the web service then returns the pdf.
Would that make sense?
What would be the best way to implement this?
cheers,
Terry
Use file IO to read the PDF in and have your webmethod return it as a byte[] array. For example like this:byte[] filebytes = null;
using (FileStream fs = File.OpenRead(fileName))
{
filebytes = new byte[fs.Length];
fs.Read(filebytes, (int)0, (int)fs.Length);
}
return filebytes;
It will be Base64 encoded and you will then be able to Save and view the file on the client.
You could also write an http-handler that returns the pdf with the appropriate mime type. This way you would not have all the processing overhead of an aspx page.