Rahul Chowdhury
Rahul Chowdhury

Reputation: 1158

How to pass a long byte array in query string?

I am passing an image byte array from my main page to handler. I can not use session. Is it possible to pass the byte array in query string I have tried

imgPreview.ImageUrl=ImageHandler.ashx?imagebytes=" + System.Text.Encoding.UTF8.GetString(fileUploader.FileBytes);

But its not working.

Upvotes: 0

Views: 7934

Answers (5)

Craig Tullis
Craig Tullis

Reputation: 10507

At the risk of creating a "me, too" answer; Base64 encoding would be the only reasonable way to do this, but it is almost certainly (as in definitely) not the way you really want to do this. Maybe if you provided a little more context, it would be easier to help with a solution. I suppose if we're talking about just tiny little images like icons it might work, but it still isn't the solution you want. The query string is not meant to pass objects around directly. It is meant to make a request for a resource. You could save your images to files on disk and reference the files directly in your URL, as others have suggested. If your images are in a database, you could put the image's id in the url, and create a page that sets the appropriate MIME type for your image, and streams out the bits to the caller. If you did that, you might end up with a url that looks like http://servername/image.aspx?id=123 which would, to the caller, be exactly the same thing functionally as http://servername/image.jpg. In fact, if you did that, then you could also choose to employ additional query string parameters to specify things like the output size for the image, or even the type of the image (in which case your aspx page would resize the image or convert it between formats).

Upvotes: 0

Try to store the image into a location into the file system, pass the location via get and retrieve the image from the second script, query string request have a length limit and an image is a lot of bytes.

Upvotes: 0

Lloyd
Lloyd

Reputation: 29668

It's not working because, amongst the many issues with this you are using UTF-8 to encode binary data. You'd want to use base64 encoding really and then after that you'd probably also need to URL encode that.

Both will inflate your url length CONSIDERABLY and probably will hit limits.

You need to tag the image with an ID of some sort backend and have that passed in the URL instead.

You could store the image on the file system if you cannot use session storage (which you shouldn't for images anyhow) or a database.

Upvotes: 2

Davut Gürbüz
Davut Gürbüz

Reputation: 5746

This is not the right way of transferring a long expression on querystring.

You should make a httprequest. https://stackoverflow.com/a/5527349/413032

You should manipulate the length

Upvotes: 0

Ali
Ali

Reputation: 1409

No you cannot do so. Query string length has limits. And these limits vary from browser to browser.

Upvotes: 0

Related Questions