JS或JQ能通过ajax实现图片下载吗?

yxhkami 2016-04-28 03:19:23
图片就保存在服务器上面,但是用a标签的href就是直接打开图片。怎么才能弹出另存为呢?
...全文
651 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
墨白一凡 2016-04-29
  • 打赏
  • 举报
回复
引用 11 楼 yxhkami 的回复:
[quote=引用 10 楼 xiaohuatongxue 的回复:] [quote=引用 9 楼 yxhkami 的回复:] [quote=引用 8 楼 xiaohuatongxue 的回复:] 浏览器是识别JPG,JPEG。。。等图片格式,你只能通过ATTACHMENT LOAD的方式进行打开。
     /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="s_fileName"></param>
        void loadimgs(string files)
        {
            Response.ContentType = "application/ms-download";
            string s_path = Server.MapPath("/imgages") +"/"+files;
            System.IO.FileInfo file = new System.IO.FileInfo(s_path);
            Response.Clear();
            Response.AddHeader("Content-Type", "application/octet-stream");
            Response.Charset = "utf-8";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Clear();
            Response.End();
        }
file是文件名 s_path是绝对路径+file 点击A标签把文件名传到后台,通过后台处理
在action里面执行到这一步ajax就报错了。。[/quote] 看看你的s_path返回的路径?是什么?看下控制台,throw exception是什么?[/quote] 路径没问题,在网站上都能打开。报了parsererror这个错[/quote]
http://bbs.csdn.net/topics/390577777
yxhkami 2016-04-29
  • 打赏
  • 举报
回复
引用 10 楼 xiaohuatongxue 的回复:
[quote=引用 9 楼 yxhkami 的回复:] [quote=引用 8 楼 xiaohuatongxue 的回复:] 浏览器是识别JPG,JPEG。。。等图片格式,你只能通过ATTACHMENT LOAD的方式进行打开。
     /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="s_fileName"></param>
        void loadimgs(string files)
        {
            Response.ContentType = "application/ms-download";
            string s_path = Server.MapPath("/imgages") +"/"+files;
            System.IO.FileInfo file = new System.IO.FileInfo(s_path);
            Response.Clear();
            Response.AddHeader("Content-Type", "application/octet-stream");
            Response.Charset = "utf-8";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Clear();
            Response.End();
        }
file是文件名 s_path是绝对路径+file 点击A标签把文件名传到后台,通过后台处理
在action里面执行到这一步ajax就报错了。。[/quote] 看看你的s_path返回的路径?是什么?看下控制台,throw exception是什么?[/quote] 路径没问题,在网站上都能打开。报了parsererror这个错
墨白一凡 2016-04-29
  • 打赏
  • 举报
回复
引用 9 楼 yxhkami 的回复:
[quote=引用 8 楼 xiaohuatongxue 的回复:] 浏览器是识别JPG,JPEG。。。等图片格式,你只能通过ATTACHMENT LOAD的方式进行打开。
     /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="s_fileName"></param>
        void loadimgs(string files)
        {
            Response.ContentType = "application/ms-download";
            string s_path = Server.MapPath("/imgages") +"/"+files;
            System.IO.FileInfo file = new System.IO.FileInfo(s_path);
            Response.Clear();
            Response.AddHeader("Content-Type", "application/octet-stream");
            Response.Charset = "utf-8";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Clear();
            Response.End();
        }
file是文件名 s_path是绝对路径+file 点击A标签把文件名传到后台,通过后台处理
在action里面执行到这一步ajax就报错了。。[/quote] 看看你的s_path返回的路径?是什么?看下控制台,throw exception是什么?
yxhkami 2016-04-29
  • 打赏
  • 举报
回复
引用 8 楼 xiaohuatongxue 的回复:
浏览器是识别JPG,JPEG。。。等图片格式,你只能通过ATTACHMENT LOAD的方式进行打开。

     /// <summary>
/// 文件下载
/// </summary>
/// <param name="s_fileName"></param>
void loadimgs(string files)
{
Response.ContentType = "application/ms-download";
string s_path = Server.MapPath("/imgages") +"/"+files;
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
Response.Clear();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.Clear();
Response.End();
}


file是文件名

s_path是绝对路径+file

点击A标签把文件名传到后台,通过后台处理




在action里面执行到这一步ajax就报错了。。
墨白一凡 2016-04-29
  • 打赏
  • 举报
回复
浏览器是识别JPG,JPEG。。。等图片格式,你只能通过ATTACHMENT LOAD的方式进行打开。
     /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="s_fileName"></param>
        void loadimgs(string files)
        {
            Response.ContentType = "application/ms-download";
            string s_path = Server.MapPath("/imgages") +"/"+files;
            System.IO.FileInfo file = new System.IO.FileInfo(s_path);
            Response.Clear();
            Response.AddHeader("Content-Type", "application/octet-stream");
            Response.Charset = "utf-8";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Clear();
            Response.End();
        }
file是文件名 s_path是绝对路径+file 点击A标签把文件名传到后台,通过后台处理
yxhkami 2016-04-29
  • 打赏
  • 举报
回复
有什么好办法没?
yxhkami 2016-04-28
  • 打赏
  • 举报
回复
引用 4 楼 showbo 的回复:
download属性有兼容性问题,最好就是通过服务器端设置content-disposition为attachment来弄
怎么弄啊?不懂啊。。最好能用ajax来实现。
Go 旅城通票 2016-04-28
  • 打赏
  • 举报
回复
download属性有兼容性问题,最好就是通过服务器端设置content-disposition为attachment来弄
xuzuning 2016-04-28
  • 打赏
  • 举报
回复
不可以的,因为在 web 方式下是不予许操作本地文件的 这是浏览器厂商为了保护你的隐私(当然也是为了他的信誉)采取的安全措施,不要试图绕开他
xuzuning 2016-04-28
  • 打赏
  • 举报
回复
只有 Firefox 和 Chrome 支持 download 属性。 所以只是花瓶而已
yxhkami 2016-04-28
  • 打赏
  • 举报
回复
引用 1 楼 slwsss 的回复:
http://www.w3school.com.cn/tags/att_a_download.asp
我不信这么简单。

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧