2016-08-12 109 views
0

想下载一个文件,“保存文件对话框”应该会跳出选择的文件夹中保存文件的javascript:从服务器下载文件:打开文件对话框,选择文件夹来保存文件

我试着与window.locationwindow.location.assignwidow.open功能。 一切导致打开新的一页!并不能看到任何“保存文件夹对话框中的”弹出!

下载的文件名是.cfg扩展名,该文件可以是二进制或JSON (文本文件)。会不会有什么区别,如果该文件是二进制或文本?

$(document).ready(function() { 
 

 
    $("#DownloadFile").click(function() { 
 
    // // hope the server sets Content-Disposition: attachment! 
 
    var \t urlData= window.location.protocol + "//" +  window.location.host + "/" + "download/testdown.cfg"; 
 
    alert("download file: " + urlData); 
 
    retval = fileExists(urlData); 
 
    if(retval) { 
 
     alert("file exists !!"); 
 
     window.location = 'download/testdown.cfg'; 
 
     // window.location.assign(urlData); 
 
     // window.open(urlData, 'Download'); 
 

 
    }else{ 
 
     alert("File not exists !"); 
 
    } 
 
    }); 
 

 

 

 

 

 
    function fileExists(url) { 
 
    alert(" url : " + url); 
 
    if(url){ 
 
     alert(" url : " + url); 
 
     var req = new XMLHttpRequest(); 
 
     req.open('HEAD', url, false); 
 
     req.send(); 
 
     // return req.status==200; 
 
     return true; 
 
    } else { 
 
     return false; 
 
    } 
 
    } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table table-condensed table-bordered table-striped volumes tabcenter" 
 
     style="align:center; margin:5px; width:98%"> 
 

 
    <tbody> 
 
    <tr> 
 
     <td> 
 
     <div class="row"> 
 
      <h1>Choose File Type</h1> 
 
      <label class="radio-inline"> 
 
      <input name="radioGroup" id="radio1" value="option1" type="radio"> Binary Data 
 
      </label> 
 
      <label class="radio-inline"> 
 
      <input name="radioGroup" id="radio2" value="option2" checked="" type="radio"> Json Data 
 
      </label> 
 

 
     </div> 
 
     </td> 
 

 
     <td > 
 
     <button type="submit" id="DownloadFile" >Download!</button> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+1

您无法控制此行为。 –

+0

https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1 – epascarello

回答

相关问题