急呀!!怎么没有人会呀?????
问题是这样的,我要在客户端调用WEB页面实现文件的下载,可是点击客户端下载按钮后,弹出的保存文件对话框经常跟我捉迷藏,时有时无。各位大虾帮忙看看是怎么回事。谢谢了!在线等web端代码如下:
void Page_Load(Object sender, EventArgs e)
{
string Hid;
string company;
string year;
string filepath;
string filename;
Hid = Request.Params["HID"];
company = Request.Params["Company"];
year = Request.Params["iYear"];
filename = "Y" + year + "-Results.xls";
try
{
filepath = ("D:\\abc\\Ind") + Hid + "\\" + company ;
if (System.IO.Directory.Exists(filepath))
{
Response.Clear();
Response.Buffer = true;
//Response.ContentType = "text/html";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
Response.ContentType = "binary/vnd.ms-excel";
Response.WriteFile(filepath + "\\"+ filename);
Response.Flush();
Response.End();
Response.Write("0");
}
else { Response.Write("1"); }// 表示文件路径不存在
}
}
vb端代码如下:
Const DataPath = "http://192.168.0.141/"
Sub CheckID(URL As String)
Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
WebBrowser.Visible = False
WebBrowser.Navigate URL
Do While WebBrowser.busy
DoEvents
Loop
On Error Resume Next
If Strings.Left(WebBrowser.Document.body.innertext, 1) = "0" Then
WebBrowser.Quit
'get file
downloadfromServer
Unload Me
Exit Sub
End If
MsgBox "密码错误!", vbCritical, Title
Set WebBrowser = Nothing
End Sub
Private Sub PrintButton2_Click()
Dim Uname, PWord As String
Uname = Strings.Trim(Txt2ID.Text)
PWord = Strings.Trim(TxtPSW2.Text)
CheckID DataPath & "CheckLog.aspx?u=" & Uname & "&p=" & PWord
End Sub
Private Sub UserForm_Layout()
Txt2ID.Text = Workbooks("Main.xls").Worksheets("D").Range("IN").Value
End Sub
Public Sub downloadfromServer()
Dim FileName, phpName As String
Dim fso As New Scripting.FileSystemObject
Dim myfile As Scripting.File
Dim fs As Scripting.TextStream
' Dim DPath As String
Dim iYear As Integer
Dim HID As String '行业号
Dim Company As String '公司号
Dim Indsecret As String '行业密码前4位
Dim Year As String ' 年份
Dim CO As String
Dim COLIST As Variant
COLIST = Array("", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P")
Year = TextBox1.Text
phpName = "down.aspx"
HID = Workbooks("Main.xls").Worksheets("D").Range("IN").Value
CO = Workbooks("Main.xls").Worksheets("D").Range("CO").Text
Company = COLIST(CO)
IEGetStringRequest DataPath & phpName & "?HID=" & HID & "&Company=" & Company & "&iYear=" & Year
End Sub
Function GetFile(FileName As String) As String
Dim FileContents() As Byte, FileNumber As Integer
ReDim FileContents(FileLen(FileName) - 1)
FileNumber = FreeFile
Open FileName For Binary As FileNumber
Get FileNumber, , FileContents
Close FileNumber
GetFile = Strings.StrConv(FileContents, vbUnicode)
End Function
Sub IEGetStringRequest(URL As String)
Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
WebBrowser.Visible = False
'Dim filepath As String
WebBrowser.Navigate URL
Do While WebBrowser.busy
DoEvents
Loop
On Error Resume Next
If Left(WebBrowser.Document.body.innertext, 1) = "0" Then
MsgBox "文件下载失败!" & Err.Description, vbCritical, Title
End If
End Sub