I am retrieving a binary image from SQL Server and would like to pass this binary image from a GridView control to another page and display by using an Image control. What is the best way to do this?
I have tried to use Http.Utility (encode & decode) and it said URL is too long.
My suggestion is as follows:
1. From the details, it seems that your image is stored as a blob(binary data) in database table.
2. Instead of fetching the blob(binarydata) and passing it to the next page, you can just pass the database ID to the next page.
3. On the next page, make an AJAX request to the .NET server side code (you can put this on page load) and fetch the blob based on the database ID received from the previous page.
4. Then display this image and load it in the HTML div or img tag using the javascript success callback of the ajax request.
By the way, it is recommended to always store images on a filesystem. If you can refactor your server side code to store the images in a directory structure - that would be ideal. It may take lot of changes to your code base. If the images are not too big - you may continue with the existing approach with the solution I suggested. Generally, having large binary data in database tables - could in long term make the queries slower.
You may want to check how to set binary data fetched as an image and set it in html. Please check this link for details: Is it possible to put binary image data into html markup and then get the image displayed as usual in any browser?
Related
I'm creating images dynamically with Jquery in Frontend. Then I'm moving these pictures' to the backend through a textbox and I am splitting according to the ',' character. Now I have an array of string ids. I am trying to save images to a folder using these ids. But I have no idea how to use these ids.
Like this, I need to use these ids:
string[] image_ids_st = txbx_image_ids.Text.Split(','); //["img1","img2"]
From the given information it is actually not quite clear what your goal is.
I understand you want to save the images + ids somewhere, I guess on the server where the back-end is hosted?
This means you are making a REST call to the back-end which uses the ApiController in the System.Web.Http namespace, right? If you want to send the images to the back-end you can use Multipart Form Data or a JSON body containing the information Id and the byte stream of the image. For saving images in the back-end, you should read through this documentation https://learn.microsoft.com/en-us/dotnet/api/system.io?view=netframework-4.8.
I am attempting to retrieve data that is dynamically loaded onto a webpage using hashed links i.e. http://www.westfield.com.au/au/retailers#page=5
My question is what technology is being used to load the data onto the page?
Secondly, how would one approach retrieving this data using C#?
My attempts so far have used WebClient to download the page at this link, unfortunately the html file only contains the data from the very first page, no matter what page link i use.
What technology is being used to load the data onto the page?
JavaScript is used to load the data from a server, parse it into HTML and put it in the right place in the DOM.
Secondly, how would one approach retrieving this data using C#?
Make a request to: http://www.westfield.com.au/api/v1/countries/au/retail-chains/search.json?page=5, it will return a structured JSON document containing the data you need.
If all you need is the JSON structure, Jon's answer sounds like a good place to start.
If you want a good stack for true rendered scraping I'd use a combination of phantomjs and Selenium to help bridge it to .net.
This article is a great place to start.
I'm currently in need of develop a website that allow user to input information and images into a <textarea> then save to database. Those data will be displayed in another page.
I though of using AJAX to upload file then append the <img> to <textarea>. But this approach will produce SQL injection security threat.
So I need an advice on how to achieve this but still can get rid of SQL injection.
When the user uploads an image, just do the following and you will be safe to append the image HTML to the textarea:
Is the user upload a valid JPG/GIF/PNG/x image? (Use image libraries to verify that.)
Rename the image to something "safe" like a CRC32 of its contents + the current time in microseconds so the file name is innocuous.
Put the image with its new name in a location that can be served.
you'll find you cant upload files using ajax - you will want to find a plugin for that. I'd reccomend looking at https://github.com/blueimp/jQuery-File-Upload/wiki - although it may have too many features. Theres even an mvc3 example for it! https://github.com/maxpavlov/jQuery-File-Upload.MVC3
After you upload the image, I suppose you can trigger another ajax event to retrieve the image and then display it in a div next to the text area.
As far as security issues, I've been told allowing a user to upload a file is inherently insecure. see https://github.com/blueimp/jQuery-File-Upload/wiki/Security for more details.
My apologies for the excess links. Also, if you're storing many, many files with infrequent access, you may want to consider saving the files to disk. ie. write them to a network location and store the filename in a table (save the file on disk with a GUID).
ie. Table UserFileLocations
PK | UserFileName | DiskID
1..n | tree.jpg | //ServerPath/Folder/103c-aa34-0ac2-01cd
...
I have a FileUpload control in my aspx page and I want to display the image selected before postback so that user confirms the image to upload.
You can use this AJAX Control Toolkit Control for Image Confirmation.
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AsyncFileUpload/AsyncFileUpload.aspx
What you're looking for would require a pure client-side solution, which I do not think is possible since they do not have access to the file system(under normal circumstances).
Note that using AJAX requires actually sending the file to the server first.
This Can be done VIA HTML5 now,
http://www.html5rocks.com/en/tutorials/file/dndfiles/
When the upload is selected you want to read the file with
readAsBinaryString
Then you would need to turn that binary to Base64 so you could display it on the page,
http://www.webtoolkit.info/javascript-base64.html
Then you will need to put it into an img tag on the in the src E.G
<img src="data:{image/mime_type};base64,{base64_binary_data}" width="100" height="100" />
Where {image/mime_type} is the mime type of the image they have uploaded E.G image/png, image/jpg
And {base64_binary_data} is the readAsBinaryString after it's gone though the base64 conversion
It's not possible through HTML. But it may be possible using flash/silverlight.
I say this because I remember coming across an issue when I wanted to know up front how big a file was before the user uploaded it. Html doesn't give you the ability to know this, the user must upload the whole file before you can say how big it is.
However, the workaround was to use flash because flash seems to have permissions to grab data about a file from the local disk before sending it to the server (after the user selects the file of course). Since it can grab the filename, size, etc then I imagine you'll be able to grab the image data too and then display it.
You're going to need to use flash or silverlight to do that.
Here's an article where it's explained how to do that with flash:
http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/
I have a database which stores .png images as the sql "image" type. I have some code which retrieves these images as a byte[], and sends them to the page via the FileContentResult object in .Net. Performance is key in this application, and the images have to be retrieved and displayed as quickly as possible. My question is, can this operation be performed quicker by passing a byte stream from the database to the browser, and not at anytime storing the whole byte array in memory. If this is possible and worthwhile doing, how do I do it?
Here is the code I have so far:
// Get: /Image/Get/5
public FileResult Get(int id)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(300));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
// Get full size image by PageId.
return base.File(page.getFullsizeImage(id), "image/png");
}
And
public byte[] getFullsizeImage(int pageId)
{
return (from t in tPage
// Filter on pageId.
where t.PageId == pageId
select t.Image).Single().ToArray();
}
Thanks for any help!
A nice question.
Reality is the code required to send the image as a stream is really minimal. It is just Response.Write~~~ byte array and setting the HTTP's content-type header which must be very fast.
Now you seem to need to open up your database to the world to get it done quicker. That, being probably possible using features that allow SQL server to serve HTTP/interact with IIS (long time ago I looked at it), not a good idea so I do not believe you should take that risk.
You are already using the caching so that is cool but files being large, cache gets purged frequently.
But one thing to do is to have a local File Cache on the IIS and if image is used, it is written to the file on teh web server and from then on (until maybe next day when this is cleared) this other URL (to the static asset) is returned so requests would not have to go through the ASP.NET layer. It is not a great idea but will achieve what you need with least risk.
Changing the linq from single to first should give you nicer SQL, if PageId is the primary key you can safely assume first and single will return the same result.
Edit: Based on your comments, I think you should consider using DeepZoom from microsoft. Essentially, what this allows you to do is generate a specialized image file on the server. When a user is browsing the image in full view, just the couple of million or so pixels that are displayed on the screen are sent to the browser via AJAX. Then when the user zooms in, the appropriate pixels for the zoom level and x and y axis are streamed out.
There is a DeepZoom Composer which can be accessed via the command line to generate these image files on demand and write them to a network share. Your users will be really impressed.
Take a look at this example. This is a massive image - Gigabytes. in about the middle of the image you will see some newspaper pages. You can zoom right in and read the articles.
End of Edit
Do you have to have images with a large file size? If they are only meant for displaying in the browser, they should be optimized for the web. All main image editing applications have this ability.
If you do need the large file size, then you could provide optimized images and then when the user clicks on the image, allow them to download the full file. They should expect this download to take some time.
In Photoshop, the task is "Save for web". There is a similarly named plugin for Gimp.
I know that this doesn't answer your direct question ("can this operation be performed quicker by passing a byte stream"), but it might help solve your problem.