网址:http://115.29.99.116:2015/
网站是无聊时做的玩的。现在有两个问题请求众大神帮助。
两个问题主要是涉及通过url下载音乐到本地:
1.在html中<a>
的href
写上歌曲的下载地址,点击时的确会调转到下载的页面,可是还是需要自己手动在地址栏中按一下回车键才能下载。
2.网站也采用的从自己的服务端下载歌曲,使用request获得下载流再传给前端,但是这种做法却使得文件名丢失,具体代码为:
getPipe: function (req, res) {
var url = req.param('url');
if (!url) {
return res.send('missing some param')
}
var options = {
url: url,
headers:{
'Referer': 'http://www.baidu.com'
}
};
var x = request(options);
req.pipe(x);
x.pipe(res);
}
351466122400128.mp3?xcode=5e62b83bd75f13734b099fe349090fd59760ee20d71d7670 /data2/music/109017317 GET 403 Forbidden text/html Other ============================================================ 351466122400128.mp3?xcode=5e62b83bd75f13734b099fe349090fd59760ee20d71d7670 /data2/music/109017317 GET 200 OK audio/mpeg Other ============================ 头一个是app跳转的,后一个是手动输的,原因就是mimetype 解决方法:新建一个界面,使用ajax。。设置mimetype
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="jquery.min.js"></script>
<script>
$(function(){
var url = "http://180.76.22.36/data2/music/44642668/44265869190800128.mp3?xcode=3d1e435b4a60878d603e4de935a2edff10dd908877013d63";
$.ajax(url, {
"mimeType":"audio/mpeg"
});
});
</script>
</body>
</html>
请求报错:
Request URL:http://180.76.22.36/data2/music/44642668/44265869190800128.mp3?xcode=3d1e435b4a60878d603e4de935a2edff10dd908877013d63
Request Headers
Provisional headers are shown
Accept:*/*
Origin:http://localhost:63342
Referer:http://localhost:63342/demo2/index.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:43327100-7886-C707-529B-07CB2F3C64E6
Query String Parametersview sourceview URL encoded
xcode:3d1e435b4a60878d603e4de935a2edff10dd908877013d63
@tsq 之前的方法并没有验证, 使用jquery1.11是可以下载的,但出现的问题是…无法保存。 so 我找到了一个完美的方法 代码来自网络 function download_file(url) { if(typeof(download_file.iframe)== “undefined”) { var iframe = document.createElement(“iframe”); download_file.iframe = iframe; document.body.appendChild(download_file.iframe); } download_file.iframe.src = url; download_file.iframe.style.display = "none";
} 经验证可以下载并能保留文件名
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button onclick="download_file()">下载</button>
<script>
function download_file() {
var url = "http://180.76.22.36/data2/music/122140204/7319923154800128.mp3?xcode=676f1ec00e7267db9cab92158c32d2109ac2e4d7e68828ec";
if (typeof(download_file.iframe) == "undefined") {
var iframe = document.createElement("iframe");
download_file.iframe = iframe;
document.body.appendChild(download_file.iframe);
}
download_file.iframe.src = url;
download_file.iframe.style.display = "none";
}
</script>
</body>
</html>
结果报错403
Remote Address:180.76.22.36:80
Request URL:http://180.76.22.36/data2/music/122140204/7319923154800128.mp3?xcode=676f1ec00e7267db9cab92158c32d2109ac2e4d7e68828ec
Request Method:GET
Status Code:403 Forbidden
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2
Connection:keep-alive
Host:180.76.22.36
Referer:http://localhost:63342/lowdb/1.html
User-Agent:Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
Query String Parametersview sourceview URL encoded
xcode:676f1ec00e7267db9cab92158c32d2109ac2e4d7e68828ec
Response Headersview source
Connection:close
Content-Encoding:gzip
Content-Type:text/html
Date:Sat, 06 Sep 2014 23:59:04 GMT
Server:JSP3/2.0.0-b
使用的是chrome浏览器。 无论如何,非常感谢你的帮助!!!
@tsq <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> </head>
<body> <button onclick="ajax2()">下载</button> <iframe src="" id="GoToUrl" height="1" width="100%" frameborder=0;></iframe> <script> var url = "http://180.76.22.36/data2/music/122140204/7319923154800128.mp3";function ajax2(){ var sd=document.getElementById(“GoToUrl”); var SdWindow=sd.contentWindow; var TheHtml=’<form class="ftext" name="payFM" id="payFM" method="get" action="'+url+'"><input type="hidden" name="xcode" value="676f1ec00e7267db9cab92158c32d2109ac2e4d7e68828ec"></form>'; SdWindow.document.body.innerHTML=TheHtml; SdWindow.document.forms[0].target="_top" SdWindow.document.forms[0].submit(); }
</script> </body> </html>