【问题标题】:Returning an exe file from server and downloading on client via browser从服务器返回一个exe文件并通过浏览器在客户端下载
【发布时间】:2023-04-03 13:42:01
【问题描述】:

我正在尝试将 exe 文件从服务器发送到客户端。 文件内容以字节数组的形式出现。 然后我试图再次在客户端机器上重新创建.exe 文件。 在服务器端,我将文件内容返回为 'application/octet-stream', 'Content':bytearray

我正在使用以下类型的 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()];
    }

【问题讨论】:

  • 为什么在浏览器中用JS构建文件?为什么不直接让浏览器从服务器下载呢?
  • 我不能直接从服务器下载它,因为我只能从服务器返回数据并以某种方式重用数据并开发 exe
  • 我不明白“下载”和“从服务器返回数据”有什么区别。
  • 服务端提供的api是用groovy编写的。这个api有直接向浏览器发送数据而不是文件的方法。所以在客户端我们收集从服务器接收到的字节并尝试使用上面的代码以exe的形式下载它
  • 服务器如何发送文件的字节? HTTP 标头是什么?

标签: java javascript html ajax


【解决方案1】:

我已经通过以下方式解决了这个问题: 服务器端代码:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多