Code67使用JavaScript下载文件

来源:互联网 发布:公司商标制作软件 编辑:程序博客网 时间:2024/06/07 14:39

下面代码显示如何使用JavaScript下载文件。

<HTML>

    <HEAD>

       <title>Flash with Java script file download example.</title>

        <METAhttp-equiv="PRAGMA" content="NO-CACHE">

        <linkrel="stylesheet" href="http://huijianight.blog.163.com/blog/styles.css">

    <metahttp-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD>

<body>

 

<h3>Download mode with extrenal interface(JavaScript+HTML)</h3>

Example demonstrates how to replace Flash interface andfunctionality with your custom HTML and Java Script code.

    <table>

       <tr>

           <td align="center">

               <font style="font-size:10px;font-family:Verdana, Arial,Helvetica, sans-serif" color="#494949">

                   You need at least 8 version of Flash player. Download last version

                   <a target="_blank" href="http://www.adobe.com/shockwave/download/alternates/">here</a>

               </font><br>

     <!--FLASH UPLOAD CODE. START -->

     <divalign="left" id="FlashUpload" style="">

<OBJECT id="FlashFilesUpload"codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"

       width="450" height="350"classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" VIEWASTEXT>

        <!--Replace symbols " with the " at all parameters values and

        symbols"&" with the "%26" at URL values or & at othervalues!

        The sameparameters values should be set for EMBED object below. -->

    <PARAMNAME="FlashVars" VALUE="downloadMode=Yes

&useExternalInterface=Yes

&filesToDownload=Images/landscape.jpg|landscape.jpg|48546;Images/frog.jpg|frog.jpg|45119">

    <PARAMNAME="BGColor" VALUE="#F8F6E6">

    <PARAMNAME="Movie" VALUE="ElementITMultiPowUpload1.7.swf">

    <PARAMNAME="Src" VALUE="ElementITMultiPowUpload1.7.swf">

    <PARAMNAME="WMode" VALUE="Window">

    <PARAMNAME="Play" VALUE="-1">

    <PARAMNAME="Loop" VALUE="-1">

    <PARAMNAME="Quality" VALUE="High">

    <PARAMNAME="SAlign" VALUE="">

    <PARAMNAME="Menu" VALUE="-1">

    <PARAMNAME="Base" VALUE="">

    <PARAMNAME="AllowScriptAccess" VALUE="always">

    <PARAMNAME="Scale" VALUE="ShowAll">

    <PARAMNAME="DeviceFont" VALUE="0">

    <PARAMNAME="EmbedMovie" VALUE="0">       

    <PARAMNAME="SWRemote" VALUE="">

    <PARAMNAME="MovieData" VALUE="">

    <PARAMNAME="SeamlessTabbing" VALUE="1">

    <PARAMNAME="Profile" VALUE="0">

    <PARAMNAME="ProfileAddress" VALUE="">

    <PARAMNAME="ProfilePort" VALUE="0">

   

    <!-- Embedfor Netscape,Mozilla/FireFox browsers support. Flashvars parameters are thesame.-->

        <!--Replace symbols " with the " at all parameters values and

        symbols"&" with the "%26" at URL values or & at othervalues! -->

    <embed bgcolor="#F8F6E6"id="EmbedFlashFilesUpload"src="http://huijianight.blog.163.com/blog/ElementITMultiPowUpload1.7.swf" quality="high"pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"   type="application/x-shockwave-flash" width="450"height="350" flashvars="downloadMode=Yes

&useExternalInterface=Yes

&filesToDownload=Images/landscape.jpg|landscape.jpg|48546;Images/frog.jpg|frog.jpg|45119">

   </embed>

  </OBJECT>

 

     </div>

     <!--FLASH UPLOAD CODE. END -->

    

     <!-- JAVASCRIPT INTERFACE CODE. START -->

     <divalign="left" id="JSUpload"style="width:1px;height:1px;visibility:hidden;">

      <selectid="fileslist" name="fileslist"style="HEIGHT:200px;WIDTH:300px;" multiple>

     </select>

        <tablestyle="border:solid 1px #000033;" width="300"height="25" cellpadding="0" cellspacing="0">

           <tr>

               <td>

                   <table id="rowProgress" bgcolor="#0033ff"width="1" height="25" cellpadding="0"cellspacing="0">

                        <tr>

                            <td></td>

                       </tr>

                   </table>

               </td>

           </tr>

       </table>

    <divid="lable"> </div>

      <inputtype="Button" value="Download selected"name="flashInfoButton"onClick="javascript:downloadsel();"/>

    <inputtype="Button" value="Download all"name="flashInfoButton"onClick="javascript:downloadall();"/>

    </div>

   

    <!-- JAVASCRIPT INTERFACE CODE. END -->

   

               </td>

         </tr>

   </table>

     

       

      <scriptlanguage="JavaScript">

var Flash;

var uploadedfilescount = 0;

 

window.onload = function() {   

   InitFlashObj();

}

 

// 初始化 "flash" 对象

function InitFlashObj(){

   if(document.embeds && document.embeds.length>=1)

        Flash =document.getElementById("EmbedFlashFilesUpload");

    else

        Flash =document.getElementById("FlashFilesUpload");

}

 

/*

   MultiPowUpload_onMovieLoad. 如果浏览器支持外部接口,当影片加载是调用

     编程接口是Flash动画和JavaScript直接通信

*/       

function MultiPowUpload_onMovieLoad(){

   InitFlashObj(); // 有时在window.onload事件中没有初始化Flash

    Flash.width =1;

    Flash.height= 1;

   

    var JSUpload= document.getElementById("JSUpload");

   JSUpload.style.visibility = "visible";

   JSUpload.style.width = 350;

   JSUpload.style.height = 300;

   

    syncLists();

}

 

// 同步Flash文件列表和HTML列表

function syncLists(){

    var fileslist= document.getElementById("fileslist");

   clearListBox(fileslist);   

    var list =Flash.fileList();

    var i = 0;

    for(i=0;i<list.length; i++) {

       fileslist.options[fileslist.options.length] = new Option(list[i].name +" (" + list[i].size + " bytes)", i, false, false );

    }

}

 

// 清除列表

function clearListBox(lstBox){       

    while(lstBox.length > 0) {

       lstBox.remove(0);

    }

}

 

// 修改HTML进度条状态

function updateProgressBar(percentDone){

    varrowProgress = document.getElementById("rowProgress");

   if(percentDone>=1)

          rowProgress.width = percentDone + "%";

    else

       rowProgress.width = "1";

}

 

//MultiPowUpload_onProgress. 定期调用文件上传或下载操作

function MultiPowUpload_onProgress(type, index,fileBytesLoaded, fileBytesLength, totalBytesLoaded, totalBytesLength){

   varPercentDone = new Number((totalBytesLoaded/totalBytesLength)*100).toFixed(1);

  document.getElementById("lable").innerHTML = PercentDone +"% downloaded" + " (" + uploadedfilescount + "files)"

  updateProgressBar(PercentDone)

}

//MultiPowUpload_onError. 当输入/输出错误发生或因为HTTP错误引起上传/下载失败时调用

function MultiPowUpload_onError(type, index, error){

  window.alert(error);

}

//MultiPowUpload_onComplete. 当上传或下载单个文件操作成功时调用

function MultiPowUpload_onComplete(type, index){

   uploadedfilescount++;

}

//MultiPowUpload_onCompleteAbsolute. 当上传或下载所有文件成功是调用

function MultiPowUpload_onCompleteAbsolute(type,totalBytesLoaded){

 document.getElementById("lable").innerHTML = "Downloadcomplete! Total bytes " + totalBytesLoaded + " (" +uploadedfilescount + " files)";

}

//下载列表中选中的文件

function downloadsel(){

   uploadedfilescount = 0;

    var fileslist= document.getElementById("fileslist");

    var selind =new Array();

    varselcount=0;

    for(i=0;i<fileslist.options.length; i++) {

       if(fileslist.options[i].selected == true) {

           selind[selcount] = i;

           selcount++;   

        }   

    }

   Flash.downloadFiles(selind);

}

//下载列表中所有文件

function downloadall(){

   uploadedfilescount = 0;

   Flash.downloadAll();

}

</script>

<hr>

<p>

<a target="_blank"href="http://www.element-it.com/quickstart/util/srcview.aspx?path=/Examples/MultiPowUpload/JavaScriptDownload.src">Viewsource code</a>

</p>

</body>

</HTML>

原创粉丝点击