我有张图片是从canvas.toDateUrl获得的,如何点击个按钮下载这个图片呢?
canvas.toDateUrl()获得的data:image/png;base64url =data:image/png;base64,(后面省略一大推代码类似:X3SKVEgQEAUFAEBAEBIFsIfD/AeiDbalt5v6hAAAAAElFTkSuQmCC)
<a href=url> 点击超链接是到1个新页面,上面有张图片,右键另存为才能下载
这种情况 <img src=url> 得到图片,也要右键另存为
我希望直接点1个button就下载这个图片,最好是能选文件路径的!
希望写的详细点啊!
$("#Button1").click(function () {
$("a").attr("href", SaveImg($MyCanvas))
})
function SaveImg($jobj) {--------------------编程问答-------------------- 来个人啊 --------------------编程问答--------------------
var url = $jobj.toDataURL("image/png");
// window.open(url);
return url
}
SaveImg方法现在传$MyCanvas进去。。
得到URL了吗?
--------------------编程问答--------------------
我觉得你还是在BUTTON时。。用AJAX提交到一般处理程序下载
--------------------编程问答-------------------- 顶下! --------------------编程问答-------------------- 你要做一个Download.asp,内容如下:
<%
Response.AddHeader "content-type","application/x-msdownload"
Response.AddHeader "Content-Disposition","attachment;filename="&Url
Response.End()
%>
其中Url是附件的下载地址,建议你在Download.asp中调用数据库来获取下载地址,或者通过Session/Cookie/Request将这个下载地址传递给Download.asp.
把你的按钮链接到Download.asp就OK了!
找到这个 不懂怎么用
肯定获得 url 而且图片显示出来了! --------------------编程问答--------------------
$("Save").click(function () {
$.ajax({
type: "POST",
url: "Download.aspx",
data: "Url=" + SaveImg($MyCanvas),
// complete: function (msg) {
// $("#NameCheck").html($(msg.responseText).find("#ajaxName").val()).css({ width: 60 });
// }
});
});
function SaveImg($jobj) {
var url = $jobj.toDataURL("image/png");
// window.open(url);
return url
}
在download.aspx页
protected void Page_Load(object sender, EventArgs e)
{
string Url = Request.Form["Url"];
Response.AddHeader("content-type", "application/x-msdownload");
Response.AddHeader("Content-Disposition", "attachment;filename=" + Url);
Response.End();
}
这样不行么?
跳转到新页可以 但是必须是直接跳出下载窗口,怎么弄? --------------------编程问答--------------------
Response.AddHeader("content-type", "application/x-msdownload");
------》
Response.ContentType = "application/octet-stream";
或者用下面流的方式下载看看
FileStream fs = new FileStream(strFilePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
--------------------编程问答-------------------- toDateUrl得到是这个!
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkMAAAC8CAYAAAB7Yqp AAAgAElEQVR4Xu19B3wc13H 7BX0XgiCDSApkmIVSfVC9S5ZbrLkIttxk2U5juI4bonjmubE9t R0yw7iWRLlmV1q1iieq U2HsBQbCAAIhGlMOV/c/3wKWWy73bvcP1m/398ANw9/aVb96 XZm3jxN54vkEgQEAUFAEBAEBAFBoEAR0IQMFajkZdiCgCAgCAgCgoAgoBAQMiQTQRAQBAQBQUAQEAQKGgEhQwUtfhm8ICAICAKCgCAgCAgZkjkgCAgCgoAgIAgIAgWNgJChgha/DF4QEAQEAUFAEBAEhAzJHBAEBAFBQBAQBASBgkZAyFBBi18GLwgIAoKAICAICAJChmQOCAKCgCAgCAgCgkBBIyBkqKDFL4MXBAQBQUAQEAQEASFDMgcEAUFAEBAEBAFBoKAREDJU0OKXwQsCgoAgIAgIAoKAkCGZA4KAICAICAKCgCBQ0AgIGSpo8cvgBQFBQBAQBAQBQUDIk/JSSoCvkpIS4hPe82r96Oo6qE6qnzlzVt6sh5BXQ0ODvmfPHiotLc0reWF914LD k0fvyLhU (后面省略N多)
img src =这个 的确显示出来 而且可以右键另存为 --------------------编程问答-------------------- 对了 我用的是chrome,它的下载好像不太一样 是不是有影响? --------------------编程问答--------------------
那我的url写在哪里 没看懂呢 --------------------编程问答--------------------
private static long GetFileSize(string url, out bool progressKnown)
{
HttpWebResponse response = null;
long size = -1;
try
{
response = (HttpWebResponse) GetRequest(url).GetResponse();
size = response.ContentLength;
}
finally
{
if(response != null)
response.Close();
}
return size;
}
//下载请求
private static HttpWebRequest GetRequest(string url)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
return request;
}
补充:.NET技术 , ASP.NET