2015-03-25 65 views
0

我想從服務器發送一個exe文件到客戶端。 文件內容以字節數組的形式出現。 然後我試圖再次在客戶機上重新創建.exe文件。 在服務器端,我返回文件內容 「application/octet-stream」,'Content':bytearray從服務器返回一個exe文件,並通過瀏覽器下載到客戶端

我使用下列類型的AJAX調用來獲得文件內容。

$.ajax({ 
type : 'POST', 
url : 'https://myurl, 
cache : false, 
success : function(data) { 
var myBlob = new Blob([data], { type: "application/octet-stream" }); 
       var uri = (window.URL || window.webkitURL).createObjectURL(myBlob); 

       // var outputFile = window.prompt("Saving .log file of rows from different modalities") || 'export'; 
       var outputFile = "utility"+ '.exe' 
       var downloadLink = document.createElement("a"); 
       downloadLink.href = uri; 
       downloadLink.download =outputFile; 

       document.body.appendChild(downloadLink); 
       downloadLink.click(); 
       document.body.removeChild(downloadLink); 
cnt++; 
/* }); */ 
}, 

error : (function(message) { 
debugger; 
console.log('message ' +message) 
}), 

statusCode : { 
404 : function() { 
alert("page not found"); 
} 
} 
}); 

但是,當文件被下載時,文件的大小很大。 爲前原文件192kbs 下載的文件320 kbs 另外我運行的exe後收到以下異常: 文件的版本與您在32/64

請,如果有人可以幫助解決運行Windows版本兼容這個問題

以下是服務器端的代碼返回exe文件內容

//The context with which all SDK operations are performed. 
Context context = Context.create(); 
String modelnumber = parameters.modelnumber; 
String siteid=parameters.siteid; 
def b; 
try{ 

JSONArray arr=new JSONArray(); 
    ModelFinder mf = new ModelFinder(context); 
    mf.setName(modelnumber) 

    Model m=mf.find(); 
    if(m!=null) 
    { 
     DeviceFinder df = new DeviceFinder(context); 
     df.setModel(m) 
     df.setSerialNumber(siteid) 
     Device dev=df.find() 
     if(dev!=null) 

     { 
      UploadedFileFinder filefinder=new UploadedFileFinder(context) 
      filefinder.setDevice(dev) 
      filefinder.setFilename("/remote/notepad.exe") 
      UploadedFile temp=filefinder.find() 
      if(temp!=null) 
      { 
        File f=temp.extractFile(); 
        arr[0]=f.text 
        b=f.getBytes() 

      } 
     } 

    } 

    return ['Content-Type': 'application/binary', 'Content':b]; 
    } 
    catch(Exception e) 
    {  return ['Content-Type': 'application/text', 'Content':e.getMessage()]; 
    } 
+0

你爲什麼要建在文件JS在瀏覽器中?你爲什麼不讓瀏覽器從服務器上下載呢? – 2015-03-25 07:30:36

+0

我不能直接從服務器下載它,因爲我只能從服務器返回數據,並以某種方式重用數據和開發exe – JLyon 2015-03-25 08:36:13

+0

我不明白「下載」和「從服務器返回數據」之間的區別可能是什麼。 – 2015-03-25 08:37:06

回答

0

我在下面的方式解決了這個問題: 服務器端代碼:

JSONArray arr=new JSONArray() 
def bytes=file.getBytes() 
       arr.add(bytes) 

       return ['Content-Type': 'application/json', 'Content': arr]; 
      } 

>客戶端代碼:

value3 comes from ajax which is a byte array 

     var arr =value3 
        var byteArray = new Uint8Array(arr); 
        var a = window.document.createElement('a'); 

        a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/octet-stream' })); 
        a.download ="Utility.exe"; 

        // Append anchor to body. 
        document.body.appendChild(a) 
        a.click(); 


        // Remove anchor from body 
        document.body.removeChild(a) 
相關問題