how to download image in browser -angularjs - angularjs

I am showing image list in a html table. I have one column in the table as image name and made it as link so user can click and download the image. The image list come from database which are saved as byte array and while sending to client side I am converting it to static object as shown below
given below
FileContent = Utilities.GetString(item.FileContent);
//GetString method
public static object GetString(byte[] bytes)
{
try
{
MemoryStream stPictureSource = new MemoryStream(bytes);
var sr = new StreamReader(stPictureSource);
return sr.ReadToEnd();
}
catch(Exception ex)
{
Logger.Log(ex);
return string.Empty;
}
}
So,Can some one have an idea, how to download image in browser.
Please let me now if the question isnt clear...

Comparison your Tags included angularjs and asp.net-web-api, This sample App showing how to do file uploads with angularjs using Asp.net Web Api. Hopefully it can aid you to deal with your problem.angular-fileupload-sample

Related

Codename One: show a (secret) Image from an URL without storing and without caching

I have the following code:
EncodedImage placeholder = EncodedImage.createFromImage(FontImage.createImage(size, size, ColorUtil.GRAY), true);
String url = "...";
Date date = new Date();
URLImage qrCode = URLImage.createToStorage(placeholder, date.getTime() + ".png", url, URLImage.RESIZE_SCALE);
qrCode.fetch();
qrCodeLabel.setIcon(qrCode);
The qrCode image contains a secret that should not be saved on the Storage / FileSystem and that should not be cached in any way. It should be shown to the user only one time.
Because these requirements, of course my code doesn't work as I need, because the image is saved and cached. I prefer that the execution of the code stops until the image is downloaded, instead this code firstly shows the placeholder, then shows the image.
So, my question is which code can I use to show an image in a Label, downloading it from an url with these requirements:
no cache;
no storing;
block of the execution until the image is ready (I have a loading Dialog that I want to dispose when the image is ready).
URLImage was designed for caching. You can obviously delete the storage file but it goes a bit against the core purpose of the class.
Just use something like:
ConnectionRequest q = new ConnectionRequest(imageUrl, false) {
public void postResponse() {
EncodedImage qr = EncodedImage.create(getResponseData());
labelForQr.setIcon(qr);
parentForm.revalidate();
}
};
addToQueue(q);

Listbox default image

In an windows phone 7 application I'm populating one listbox with remote images .. since the images are not downloaded instantly I want to load a default image until the remote image are ready. What is the best way to do this?
Until now, I have the following code skelton:
public partial class RemoteImage : PhoneApplicationPage
{
ObservableCollection<Image> images = new ObservableCollection<Image> { };
public RemoteImage()
{
InitializeComponent();
listImage.ItemsSource = GetAllImages();
}
private ImageSource GetImageSource(string fileName)
{
return new BitmapImage(new Uri(fileName, UriKind.Absolute));
}
private ObservableCollection<Image> GetAllImages()
{
WebClient restClient = new WebClient();
restClient.OpenReadAsync(new Uri(#"http://www.my-api.com"));
restClient.OpenReadCompleted += new OpenReadCompletedEventHandler(onReadComplete);
return images;
}
private void onReadComplete(object sender, OpenReadCompletedEventArgs args)
{
Stream stm = args.Result;
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
RootObject ro = (RootObject)ser.ReadObject(stm);
foreach (var item in ro.items)
{
images.Add(new Image{ PhotoSource = GetImageSource(item.image.link) });
}
}
}
If you know, how many images you would need, you should create first the number of default images. Load some image file directly at your project and use it as imageSource for default images. Then, when you'll finish downloading remote images, you should set the new image source for each.
When I got the similar issue, I had some problems with defining which exactly downloaded image refers to which object on page. (As you remember the WebClient objects work asynchronously, so if you have 10 images on page and download 10 remote images at once you can't say that the first downloaded image is the first on page) To solve this you could create more complicated download method (I used a delegate to transfer the id/name of image) or use recursion (Start download method for first image, download it, set source for one on page, download next one...).

Loading image from URL in Windows Phone 7

I use below code to load image from URL in my winodws phone 7 application.
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
It is working fine for me. But image is loading asynchronously and by the time I want to show some kind of busy indicator there and if image does not exist on such URL then I want to show some default image. How can I achieve that?
I think if you are subscribed to the Image.ImageFailed Event you should be able to show to default image in case of a non-existing image.
Conditions in which this event can occur include the following:
File not found.
Invalid (unrecognized or unsupported) file format.
Unknown file format decoding error after upload.
So something like this might work for you:
image1.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(handlerImageFailed);
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
void handlerImageFailed(object sender, ExceptionRoutedEventArgs e)
{
// Show the default image
}

GWT form upload using BlobstoreService App Engine

I am using GWT and Google App Engine Java for my application. I have a profile screen where
user enters profile information like name, age and address, saves it and gets success or failure message. I developed this initial application using GWT-RPC and it worked fine. I had a new requirement where I have to store image of the user. I am using BlobstoreService to store images. This has created complications in the flow. I had to use FormPanel as it is the only way to do a FileUpload in GWT. The BlobStore service servlet expects a redirect on completion. As a result it cannot now return any status back to my GWT application once the profile is saved. Is there easy to store images using GWT along with other form fields and show a status message back to user once the profile is saved.
i struggled a lot with this problem until yesterday I figured out the solution with much help from Ikai Lan's blog. Basicaly what I did is follow his steps but with a few modifications because doing it exactly how he did it did'nt work for me:
Create a form panel : set encoding multipart, method post.
Make a GWT Remote Service that just has one method:public String getUploadURL() or something like that and in the IMPL write this:
BlobstoreService service = BlobstoreServiceFactory.getBlobstoreService();
return service.createUploadUrl("/XXX/YYY");
In XXX you must put your project path, for example mine is com.fer.pyn.PictureYourNews
In YYY you must put the servlet mapping name for a new servlet that we will have to create: I put XXX = BlobUploader, I created a BlobUploader extends HttpServlet and you have to update the web.xml.
Okey, so this is the weird part that I could'nt figure out, thing is that when we make a RPC call to getUploadURL() in the remote ervice from step 2 that returns a weird addres, like: '/_ah/img/eq871HJL_bYxhWQbTeYYoA' and that is the .fromAction you have to put in your form from step one. You need to update the form's action every time so i suggest the following:
public void initBlobStoreSession()
{
imageService.getBlobStoreUploadURL(new AsyncCallback()
{
#Override
public void onSuccess(String result) {
uploadFormPanel.setAction(result);
System.out.println("Upload Form Panel Action set");
}
#Override
public void onFailure(Throwable caught) {
//oops
}
});
}
So when you submit your fromPanel, IT WILL UPLOAD THE BLOB and you dont have to do anything, the tricky part is how to get the blob:
What you need to do now is create the YYY servlet we where talking about in step 4.
In the post method, this is important:
private BlobstoreService blobService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, BlobKey> blobMap = blobService.getUploadedBlobs(request);
BlobKey blobKey = blobMap.get(UPLOAD_WIDJET_NAME);
UPLOAD_WIDJET_NAME is the .setName for the FileUpload widjet.
What you are doing there is getting a key for yout BLob so you can reference it later.
Our next step is showing the uploaded image back to the GWT layer:
//In the same post method from step 7
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String imageURL = imagesService.getServingUrl(blobKey);
response.sendRedirect("/XXX/YYY?imgURL="+imageURL);
Now in the get method:
String imageUrl = request.getParameter("imgURL");
response.setHeader("Content-Type", "text/html");
response.getWriter().println(imageUrl);
We are done, now you just have to
uploadFormPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
#Override
public void onSubmitComplete(SubmitCompleteEvent event) {
uploadFormPanel.reset();
initBlobStoreSession();
String imageUrl = event.getResults();
Image image = new Image();
image.setUrl(imageUrl);
//if you are using jetty, leave this on
//or else it wont work
//Don't use GWT.getModuleBaseURL(), it doesnt
//work well in development mode
imageUrl.replace("http://0.0.0.0:8888/", "");
System.out.println(imageUrl);
final PopupPanel imagePopup = new PopupPanel(true);
imagePopup.setWidget(image);
// Add some effects
imagePopup.setAnimationEnabled(true); // animate opening the image
imagePopup.setGlassEnabled(true); // darken everything under the image
imagePopup.setAutoHideEnabled(true); // close image when the user clicks
imagePopup.center(); // center the image
}
});
check out upload4gwt which address uploading in GWT on AppEngine.
(disclosure: I created upload4gwt; it's not mature yet, however may be useful)
I had the same problem. As a workaround I'm using a redirection to a servlet that print a status message for the client to parse.
I'm passing the websafe string representation of the key to that result servlet.
That's a bit hackey, I'd like someone to come with a better answer, or explain why the blobstore servlet have to redirect.
Yeah, things get more complicated with uploads in GWT.
You can save the form data and image in separate RPCs, and either include a status message in the response to the image upload, or fire off a 3rd RPC when the form returns to get any status or metadata you need.

How can i get a BitmapData object out of a File object from a local jpeg file in AIR?

So I am trying to create an image gallery. My AIR application accepts files that are dragged and dropped onto a TileList component. I am using the images as icons but the problem is that they take a long time to load so i want to compress the file data first (I have that part done) The problem is that I can't figure out how to open the file and put the data into a BitmapData object.
Any ideas?
var req:URLRequest = new URLRequest(value.file.url);
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
ldr.load(req);
.
.
.
private function completeHandler(event:Event):void {
var ldr:Loader = Loader(event.target.loader);
var b:Bitmap = Bitmap(ldr.content);
}

Resources