2015-03-25 29 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) 
相关问题