用javascript读取所需文件的文件流

最近在学习使用ajax的一些控件,配合FileUpload控件读取本地文件的文件流的时候出现了一个问题.因为这里要添加一个button控件,在单击button控件的时候触发事件,读取文件流到一个textbox控件里面,这里在button事件里面取fileupload控件的FileUpload.PostFile.FileName时出现错误,在这里一直取不到FileUpload内的路径,在网上找了找发现这个还是个普遍的问题,找了两天也没有找到解决的方法.看到一个朋友在这里使用框架页来解决,所以也借用别人的方法来实现.
<iframe id="file" src="FrameFileBrowse.aspx" runat="server"></iframe>
在FrameFileBrowse.aspx页面里面不在使用updatepanel控件,结果可以满足自己的要求.后来向同事请教这个问题,一个同事建议使用javascript来实现.并写了一个javascript函数.如下:
<script>
    /*读取FileUpload控件所选择文件的文件流*/
   
 1function   Getfileinfo()
 2  {   
 3      var   fso,   f,file;   
 4      var   ForReading=1;
 5      var path=form1.FileUpload1.value;
 6      fso   =   new   ActiveXObject("Scripting.FileSystemObject");
 7      file=fso.GetFile(path);
 8      f=fso.OpenTextFile(path, ForReading);    
 9      form1.TextBox1.innerText=f.Read(file.Size);      
10      f.Close();   
11  }   
12</script>

添加一个html的button控件,<input id="Button1" type="button" value="读取文件" onclick="Getfileinfo();"  />
结果运行完全可以,在后台可以直接使用TextBox1.Text来读取所要的信息.
posted on 2007-03-29 12:16  Edwin dong  阅读(10375)  评论(9)    收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>