如何通过JavaScript保存图像 [英] How to save an image via JavaScript

查看:158
本文介绍了如何通过JavaScript保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户选择照片并单击按钮时,我想将图像文件(jpeg图像)下载到用户文件系统。到目前为止,我搜索并找到此链接以及这个

I want to download a image file (jpeg image) to the user files system when user selects a photo and clicks a button. So far I searched and found this link and also this

我在一个博客中看到,与jszip一起使用时可以启用此功能,但是没有指定更进一步。有没有人知道如何下载图像。我有图像的链接,我只是希望用户在他的系统上下载它而不是再次查询服务器。

I saw in one blog that downloadify when used with jszip can enable this feature but it didn't specify any farther on this. Does any one know how to download the images. I have the links to the images and i just want the user to download it on his system rather than again query the server.

任何人都可以给我一个例子。

Can anyone provide me with an example please.

推荐答案

最后我做到了。对于今后可能需要这个的人来说,我是如何使用jquery进行的。

Finally I did it. For anyone who may need this in the future here is how I did it using jquery

jQuery.ajax({

    url: 'http://localhost:8080/yourwebsite/servlet?img=' + document.getElementById(id).alt,
    //data: myData,
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
   // success: function() { alert("Success"); },
   // error: function() { alert('Failed!'); },
   // beforeSend: setHeader
});

我不得不遇到跨域http请求的问题,这些问题通常被大多数网站阻止除非你遵循一些漫长的过程。所以,我把它变得更简单,并在我的servlet中调用了一个get方法,并将其下载图像的图像的url传递给它。这样做更容易,如果你在同一个域上,那就更容易了不符合我的要求,这段代码对我有用:)

this I had to do come across the problem of cross domain http requests which are usually blocked by most websites unless you follow some lengthy process. So, I made it simpler and called a get method in my servlet and passed it the url of the image from which it downloaded the image.This is much easier to do and even easier then this if you are on the same domain but that didn't meet my requirements and this code worked for me :)

这篇关于如何通过JavaScript保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆