如题:
前台代码:
<!--page:检测是否注册activeX,提供下载连接-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" >
function checkReg() {
//没有注册
if ('undefined' == typeof mydll) { //检查是否注册activeX
sendReq("Handler.ashx?type=getClientSetup", null, receiveFile); //请求acx
}
}
function sendReq(url, data, stateChange) {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("您的浏览器不支持AJAX");
return;
}
}
}
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
stateChange(xmlHttp.responseText);
}
else {
alert("服务端异常");
}
}
}
xmlHttp.open("post", url, true);
xmlHttp.send(data);
} //如何接收?
function receiveFile(data) { }
</script>
<title></title>
</head>
<body onload ="checkReg()"></body>
</html>
后台代码:
<%@ WebHandler Language="C#" Class="Handler" %>using System;
using System.Web;
using System.IO;public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
if (context.Request.QueryString["type"] == null) return;
if (context.Request.QueryString["type"].ToString() == "getClientSetup")
{
string fileName = "Assignment 1 EB3991 Lit review 2011-12.doc";//客户端保存的文件名
string filePath = HttpContext.Current.Request.PhysicalApplicationPath+ "\\"+fileName;
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
context.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
}
public bool IsReusable {
get {
return false;
}
}
}
现在前台 function receiveFile(data) 收到了data,但不是保存为本地文件的对话框。
如何才能保存成文件夹。
备注: 不许在脚本中 以href的模式 透露文件的路径和名称
前台代码:
<!--page:检测是否注册activeX,提供下载连接-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" >
function checkReg() {
//没有注册
if ('undefined' == typeof mydll) { //检查是否注册activeX
sendReq("Handler.ashx?type=getClientSetup", null, receiveFile); //请求acx
}
}
function sendReq(url, data, stateChange) {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("您的浏览器不支持AJAX");
return;
}
}
}
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
stateChange(xmlHttp.responseText);
}
else {
alert("服务端异常");
}
}
}
xmlHttp.open("post", url, true);
xmlHttp.send(data);
} //如何接收?
function receiveFile(data) { }
</script>
<title></title>
</head>
<body onload ="checkReg()"></body>
</html>
后台代码:
<%@ WebHandler Language="C#" Class="Handler" %>using System;
using System.Web;
using System.IO;public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
if (context.Request.QueryString["type"] == null) return;
if (context.Request.QueryString["type"].ToString() == "getClientSetup")
{
string fileName = "Assignment 1 EB3991 Lit review 2011-12.doc";//客户端保存的文件名
string filePath = HttpContext.Current.Request.PhysicalApplicationPath+ "\\"+fileName;
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
context.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
}
public bool IsReusable {
get {
return false;
}
}
}
现在前台 function receiveFile(data) 收到了data,但不是保存为本地文件的对话框。
如何才能保存成文件夹。
备注: 不许在脚本中 以href的模式 透露文件的路径和名称
解决方案 »
- 求一段JS代码!能让链接直接调用JS函数达到管理链接地址的目的
- extjs 清空CheckboxGroup里boxLabel的勾
- 使用confirm的问题,求小代码。
- 求object标签的所有属性
- ISAPI_Rewrite做泛域名遇到的正则表达式问题,高手进来!
- 关于javascript 的回调函数问题
- 请教,怎么判断两个JS对象是否相等?
- javascript如何用正则替换\为\\
- 这个可以实现吗
- 请问,如何作出图象好象是在水里荡漾的动态画面?
- 谷歌打开 复制到txt中 解析到mysql库中
- webkit内核不能正确解析出来getElementsByTagName("yweather:forecast"),返回对象数组长度为0
“如何才能保存成文件夹”-->"如何才能弹出对话框,选择保存路径,保存文件"