I am trying to send an exe file from the server to the client. The file contents come in the form of byte array. Then i am trying to recreate the .exe
file on the client machine again. On server side I am returning file contents as ' application/octet-stream
', 'Content':bytearray
I am using an ajax call of following type to get file contents.
$.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");
}
}
});
But when the file gets downloaded the size of the file is big. for ex original file 192kbs
downloaded file 320 kbs
Also I am getting the following exception after running the exe: The version of file is not compatible with version of windows you are running on 32/64
Please if anybody can help resolve this issue
The following is the server side code to return exe file contents
//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()];
}
I have solved the problem in following manner: Server side code:
JSONArray arr=new JSONArray()
def bytes=file.getBytes()
arr.add(bytes)
return ['Content-Type': 'application/json', 'Content': arr];
}
> Client side code:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.