如题:
前台代码:
<!--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的模式 透露文件的路径和名称
解决方案 »
- IFrame 弹出模态窗体 关闭子窗体后刷新问题 ....
- 为什么validate在IE中没有效果呢?
- 我是新手,求几个关于JaveScript代码的写法,最好有注释,谢谢了
- 请问这个是什么编码格式?
- firefox中的错误问题
- 请问怎么用ASP将文本转换为JS字符串?
- 如何在JavaScript中隐藏TextArea
- 一个简单的正则表达式,我都要疯了
- 删除的问题??
- 请各位帮帮小弟,能告诉我什么是Accept.htm吗??
- 谷歌打开 复制到txt中 解析到mysql库中
- webkit内核不能正确解析出来getElementsByTagName("yweather:forecast"),返回对象数组长度为0
“如何才能保存成文件夹”-->"如何才能弹出对话框,选择保存路径,保存文件"